如果table_2的column_a中已经存在column_a的value_a(以及column_b ='x'的value_b),那么如何检查table_1?如果它不存在,请插入它并为table_2中缺少的value_a创建一个新行?
更新:(似乎有效的是:)
INSERT INTO products
(key, category)
SELECT key, category
FROM accounts
WHERE category = 'vegan'
AND NOT EXISTS (SELECT *
FROM products
WHERE accounts.key = products.key);
答案 0 :(得分:0)
为什么不将列转换为主键?您只需处理重复输入错误代码
编辑:
这里的解决方案是使用SELECT而不是WHERE(感谢degr)
INSERT INTO tableA(`id`,`column1`)
SELECT 44,'Value of Column1' -- The values you want to insert
WHERE NOT EXISTS(SELECT * FROM table2 WHERE id=40) -- check that there is no matching with the request
AND 1=1 --additional conditions
AND NOT EXISTS(SELECT * FROM Table3 WHERE id=44); --additional conditions