尽管SQL查询似乎在大多数情况下都有效,但我尝试使用EXISTS条件将比较集限制为包含仅来自导入的一个field1值的记录。似乎没有应用,所以我影响了在查询中应该忽略的记录。有人可以给我一些关于更好地进行这些查询的指导吗?
给定:MySQL数据库中的两个表:" t.import"和t.main"
第一个目标:插入t.import中存在且t.main中缺少的记录:
INSERT INTO t.main (field1,field2,field3)
SELECT field1,field2,field3
FROM t.import
WHERE
((field1,field2,field3) NOT IN (SELECT field1,field2,field3 FROM t.main))
AND
(field1 IN (SELECT DISTINCT field1 FROM t.import AS field1 ORDER BY field1));
第二个目标:如果没有来自t.import的匹配记录,则在t.main中标记记录但仅当t.main记录包含t.import中field1的各种值之一时
UPDATE t.main SET field3 = 'Dead'
WHERE (field1,field2,field3)
NOT IN (SELECT field1,field2,field3
FROM t.import)
AND
(field1 IN (SELECT DISTINCT field1 FROM t.import AS field1 ORDER BY field1))
AND
field1 != 0;