怎样查询包含在A表中但不在B表中的数据?
假设有两个数据表A和B,A表如下idname1Bob3Tom4July5LilyB表如下idage220350433635方法一select * from A where id not in (select id from B);方法二select * from A where (select count(1) from B where B.i.
·
假设有两个数据表A和B,
A表如下
id | name |
1 | Bob |
3 | Tom |
4 | July |
5 | Lily |
B表如下
id | age |
2 | 20 |
3 | 50 |
4 | 33 |
6 | 35 |
方法一
select * from A where id not in (select id from B);
方法二
select * from A where (select count(1) from B where B.id=A.id)=0;
两种方法的结果都是如下
id | name |
1 | Bob |
5 | Lily |
但是方法二要比方法一快很多,所以推荐方法二。
更多推荐
已为社区贡献1条内容
所有评论(0)