MySQL-如何获取表A中表B中不存在的值的列表? [英] MySQL - How to get a list of values in Table A that don't exist in Table B?
本文介绍了MySQL-如何获取表A中表B中不存在的值的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
**Table A**
1
2
3
4
5
6
**Table B**
2
3
5
如何为仅在表B中存在的条目ID选择?在此示例中,我正在寻找一个返回1、4和6的查询.
How can I select for entry IDs that only exist in Table B? In this example, I'm looking for a query that returns 1, 4, and 6.
推荐答案
假定该列的名称为'id',或者:
Assuming the column is named 'id', either:
SELECT *
FROM tableA a
WHERE NOT EXISTS (SELECT 1 FROM tableB WHERE id = a.id)
或
SELECT *
FROM TableA
WHERE id NOT IN (SELECT id FROM tableB)
您可能需要进行测试以查看效果最佳. MySQL可能有点不可预测.
You will probably need to test to see which performs best. MySQL can be a bit unpredictable.
这篇关于MySQL-如何获取表A中表B中不存在的值的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文