on-duplicate-key

UPON DUPLICATE KEY increment multiple columns?

我与影子孤独终老i 提交于 2019-12-11 18:27:43
问题 Im running a database log and every day I log on a new row. My Mysql query therefore checks if the day (date (the unique key)) already exists, and if so, it tries to increment all the loggable values of the log-row by one. If the date record doesnt eyist yet, it will create a new row. My SQL query is: INSERT INTO `log` (`date`,`hits`,`stale`) VALUES ('2012-03-06',1,1) ON DUPLICATE KEY UPDATE `hits`=`hits`+1,`stale`=`stale`+1 WHERE `date`='2012-03-06';" All columns have 0 as default value, so

How to INSERT INTO…SELECT with ON DUPLICATE KEY

非 Y 不嫁゛ 提交于 2019-12-11 04:59:47
问题 I have two tables with identical structure. Table A contains all the current ads, Table B contains the archived ads. Column 1 (ad_id) is Primary Key, AI, INT. Table engine is MyISAM. I need to copy all the table A ads preceding a certain date to the archive, table B. My goal is that all fields except ad_id are duplicated, ad_id should get auto-incremented. Here is what I have attempted: INSERT INTO B`(`ad_id`, `ad_advertiser`, `ad_ln`, `ad_expire`) SELECT * FROM A WHERE YEAR( ad_expire ) <=

MySQL “Insert … On Duplicate Key” with more than one unique key

时光怂恿深爱的人放手 提交于 2019-12-10 21:22:48
问题 I've been reading up on how to use MySQL insert on duplicate key to see if it will allow me to avoid Selecting a row, checking if it exists, and then either inserting or updating. As I've read the documentation however, there is one area that confuses me. This is what the documentation says: If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed The thing is, I don't want to know

MS Access: how to achieve ON DUPLICATE KEY UPDATE?

喜欢而已 提交于 2019-12-10 11:55:21
问题 Trying to avoid separate queries to check for key existance and then inserting or updating accordingly. From Googling around it seems that Access doesn't support SQL server's MERGE either. Thanks 回答1: Actually, Access does support "Upserting" as it is sometimes called. I found this solution in a book called Microsoft Access Solutions - Tips, Tricks and Secrets from Microsoft Access MVPs written by Arvin Meyer and Douglas J. Steele. Here's a query that takes the new data from the Contacts1

Update Multiple Rows mysql

微笑、不失礼 提交于 2019-12-08 11:12:52
问题 I have a table tbl |id|points| 1 15 2 35 3 445 4 42 Now if i have an array like array (2=>10,3=>825,4=>48) And i want to change the points so that the tbl looks like this. |id|points| 1 15 2 10 3 825 4 48 I can change the values using multiple queries but can anyone show how to do this with one single query ? 回答1: Use a case statement... Update tbl set points = CASE WHEN ID = 1 then 05 when ID = 2 then 10 when ID = 3 then 825 when ID = 4 then 48 END Working fiddle: http://sqlfiddle.com/#!9

Why mysql INSERT … ON DUPLICATE KEY UPDATE can break RBR replication on a master / master configuration

本秂侑毒 提交于 2019-12-07 10:42:35
问题 here is the problem: 2 MySQL 5.5 servers Row based replications + master master Writes are on both servers (both active) autoinc trick (1 server odd, the other one even) I have a table like byUserDailyStatistics: id (PK + AUTO INC) date idUser metric1 metric2 UNIQUE(idUser, date) All requests are INSERT INTO byEmailDailyStatistics (date, idUser, metric1, metric2) VALUES (:date, :user:, 1, 1) ON DUPLICATE KEY UPDATE metric1 = metric1 + 1, metric2 = metric2 +1 And sometimes, the replication

Why mysql INSERT … ON DUPLICATE KEY UPDATE can break RBR replication on a master / master configuration

泄露秘密 提交于 2019-12-05 16:42:39
here is the problem: 2 MySQL 5.5 servers Row based replications + master master Writes are on both servers (both active) autoinc trick (1 server odd, the other one even) I have a table like byUserDailyStatistics: id (PK + AUTO INC) date idUser metric1 metric2 UNIQUE(idUser, date) All requests are INSERT INTO byEmailDailyStatistics (date, idUser, metric1, metric2) VALUES (:date, :user:, 1, 1) ON DUPLICATE KEY UPDATE metric1 = metric1 + 1, metric2 = metric2 +1 And sometimes, the replication breaks with message like could not execute Write_rows event on table stats.byUserDailyStatistics;

Doctrine: ON DUPLICATE KEY UPDATE

最后都变了- 提交于 2019-12-05 00:03:55
How can I write an INSERT doctrine query with option ON DUPLICATE KEY UPDATE ? The problem is that this is a MySQL specific problem so it will not be directly covered by Doctrine. As a comment mentioned, you would need to write a RawSQL Query for this. This would be the easiest way. If you want it more sophisticated and truely DB independent, look into Events and it's possibilities. Before the actual query is executed, you can check for an existence and if it exists, act accordingly. An ORM/PHP independent way is to write a stored procedure/trigger that handles this problem database side.

EF6 CodeFirst My [Key] Id Column is not auto-incrementing as an identity column should

匆匆过客 提交于 2019-12-03 16:34:26
问题 I have several classes that I need to derive from a common base class which holds an Id. Ignoring the all but one of these for the moment, let's say we have: public class MyBase { [Key] public int Id { get; set; } } public class MyName : MyBase { public string Name { get; set; } } my context (DataContext) looks like this: public DbSet<MyName>MyNames { get; set; } // to avoid having EF make a MyBases table and instead map // MyBase.Id into MyNames and my other derived classes I do this ...

PHP Email if SQL entry is new, no email if update

*爱你&永不变心* 提交于 2019-12-02 09:36:22
问题 I have an email that gets sent out (via PHPMailer) when a new user is added to a database. The DB is updated if the email (key) already exists with the ON DUPLICATE KEY UPDATE function. However, I don't want the email to send if it is updated, only when it's new. How can I check (with PHP) if the SQL result was an update or an insert, and then use an IF statement to send/not send the email? $sql = "INSERT INTO premium_waitinglist (date, email, ip) VALUES ('".$serverDate."', '".$_POST['user