这是我的餐桌计划:
id
- >主要与AI
idCheckedData
- >因为包含多个表ID所以不能唯一
tb_name
- > idCheckedData容器的tabel名称
stats
- >检查与否
我想从spesific idCheckedData
更新tb_name
表,但是如果没有idCheckedData存在,我想自动插入给予此表的数据
我尝试过:
INSERT INTO `tb_stats` (`idCheckedData`,`tb_name`,`stats`) VALUES (12,'tb_head','checked'),(13,'tb_head','checked'),(14,'tb_head','checked') ON DUPLICATE KEY UPDATE `stats`='checked' WHERE `idCheckedData` IN (12,13,14) AND `tb_name`='tb_head';
但它似乎不能像我需要的那样工作,如果有人为我的案子找到解决方案,请帮助,谢谢
答案 0 :(得分:1)
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 of the old row. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have similar effect:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
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. 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.
If column b is also unique, the INSERT is equivalent to this UPDATE statement instead:
UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;