简体   繁体   English

MySql表插入,如果不存在,否则更新

[英]MySql Table Insert if not exist otherwise update

UPDATE AggregatedData SET datenum="734152.979166667", 
Timestamp="2010-01-14 23:30:00.000" WHERE datenum="734152.979166667";

It works if the datenum exists, but I want to insert this data as a new row if the datenum does not exist. 它的工作原理,如果datenum存在,但我想插入这个数据作为新行如果datenum不存在。

UPDATE 更新

the datenum is unique but that's not the primary key datenum是唯一的,但这不是主键

Jai is correct that you should use INSERT ... ON DUPLICATE KEY UPDATE . Jai是正确的,应该使用INSERT ... ON DUPLICATE KEY UPDATE

Note that you do not need to include datenum in the update clause since it's the unique key, so it should not change. 请注意,由于它是唯一键,因此不需要在update子句中包含datenum,因此它不应更改。 You do need to include all of the other columns from your table. 您确实需要包括表中的所有其他列。 You can use the VALUES() function to make sure the proper values are used when updating the other columns. 您可以使用VALUES()函数来确保在更新其他列时使用正确的值。

Here is your update re-written using the proper INSERT ... ON DUPLICATE KEY UPDATE syntax for MySQL: 这是您使用适用于MySQL的正确INSERT ... ON DUPLICATE KEY UPDATE语法重写的更新:

INSERT INTO AggregatedData (datenum,Timestamp)
VALUES ("734152.979166667","2010-01-14 23:30:00.000")
ON DUPLICATE KEY UPDATE 
  Timestamp=VALUES(Timestamp)

Try using this : 尝试使用这个

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 , MySQL performs an [ UPDATE`]( http://dev.mysql.com/doc/refman/5.7/en/update.html ) of the old row... 如果指定ON DUPLICATE KEY UPDATE ,并插入一行会导致UNIQUE index or PRIMARY KEY中出现重复值, MySQL performs an [ UPDATE`]( http://dev.mysql.com/doc/refman/5.7 /en/update.html )的旧行...

The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas. ON DUPLICATE KEY UPDATE子句可以包含多个列分配,以逗号分隔。

With ON DUPLICATE KEY UPDATE , the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. 使用ON DUPLICATE KEY UPDATE ,如果将行作为新行插入,则每行的受影响行值为1;如果更新了现有行,则为2;如果将现有行设置为其当前值,则为0。 If you specify the CLIENT_FOUND_ROWS flag to mysql_real_connect() when connecting to mysqld , the affected-rows value is 1 (not 0) if an existing row is set to its current values... 如果在连接到mysqld时为mysql_real_connect()指定CLIENT_FOUND_ROWS标志,如果将现有行设置为其当前值,则受影响的行值为1(而不是0)...

I had a situation where I needed to update or insert on a table according to two fields (both foreign keys) on which I couldn't set a UNIQUE constraint (so INSERT ... ON DUPLICATE KEY UPDATE won't work). 我遇到一种情况,我需要根据无法设置UNIQUE约束的两个字段(两个外键)来更新或插入到表中(因此INSERT ... ON DUPLICATE KEY UPDATE将无法工作)。 Here's what I ended up using: 这是我最终使用的内容:

replace into last_recogs (id, hasher_id, hash_id, last_recog) 
  select l.* from 
    (select id, hasher_id, hash_id, [new_value] from last_recogs 
     where hasher_id in (select id from hashers where name=[hasher_name])
     and hash_id in (select id from hashes where name=[hash_name]) 
     union 
     select 0, m.id, h.id, [new_value] 
     from hashers m cross join hashes h 
     where m.name=[hasher_name] 
     and h.name=[hash_name]) l 
  limit 1;

This example is cribbed from one of my databases, with the input parameters (two names and a number) replaced with [hasher_name], [hash_name], and [new_value]. 这个示例来自我的一个数据库,其中输入参数(两个名称和一个数字)被替换为[hasher_name],[hash_name]和[new_value]。 The nested SELECT...LIMIT 1 pulls the first of either the existing record or a new record (last_recogs.id is an autoincrement primary key) and uses that as the value input into the REPLACE INTO. 嵌套的SELECT ... LIMIT 1提取现有记录或新记录中的第一个(last_recogs.id是自动增量主键),并将其用作输入到REPLACE INTO中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM