我有一个mysql问题。比方说,我有两个具有以下值的表:
apple, orange, banana, kiwi, jackfruit, mango, strawberry
apple, orange, banana, jackfruit
。 我的问题是如何进行只选择/显示值的查询:
kiwi, mango, strawberry
有人可以为此提供示例代码吗?
答案 0 :(得分:4)
选项#1
SELECT table1.fruit FROM table1
LEFT JOIN table2
ON table1.fruit = table2.fruit
WHERE table2.fruit IS NULL
选项#2
SELECT table1.fruit FROM table1
WHERE NOT EXISTS (
SELECT 1 FROM table2
WHERE table2.fruit = table1.fruit
)
我必须看到解释计划,以回忆哪个更有效,但它实际上取决于你创建的任何索引。
答案 1 :(得分:0)
select column from table1 where column not in (select column from table2)