说我有两张桌子。 new_dogs和name_color_pairs。
name_color_pairs中的记录必须是唯一对。
new_dogs
---------
Name
Location Found
Color
Breed
name_color_pairs
---------
name
color
如何从new_dogs中选择当前不在name_color_pairs表中的名称/颜色对,以便插入它们?
答案 0 :(得分:4)
SELECT name, color FROM new_dogs
EXCEPT
SELECT name, color FROM name_color_pairs
或
SELECT name, color FROM new_dogs nd
WHERE NOT EXISTS (SELECT name, color FROM name_color_pairs ncp WHERE (ncp.name = nd.name) AND (ncp.color = nd.color))
或者最后(应该在任何地方工作):
SELECT name, color FROM new_dogs nd
LEFT JOIN name_color_pairs ncp
ON (ncp.color = nd.color) AND (ncp.name = nd.name)
WHERE ncp.name IS NULL