中易网

SQL查询语句in和exists 怎么查这两个表

答案:4  悬赏:70  
解决时间 2021-03-14 11:38
有A.B两个表
A
ID name phone
1 张三 13433333339
2 李四
3 王五 13966653222
4 赵六
5 刘七
B
IP ID city
IP1 1
IP2 1
IP3 4
IP4 4
IP5 2
问题:1、查出有IP的人2、查出有IP又有电话的人3、查出有电话但没有IP的人
1, select * from a where id in (select id from b)
2,select * from a where id in (select id from b) and phone is not null
3,select * from a where id not in (select id from b) and phone is not null

对吗?
最佳答案
基本对的,但是要注意下,null和‘’是有区别的,一般不确定是null还是‘’的时候,可以用and。
phone is not null and phone <> ''

另外,in相当于执行子查询后使用or对比,如果子查询数据量大时,用exists会快一些
全部回答
简单点说 if exists(select* from tb where name='abc') 就是这样用 上面这句话的意思是 查询表tb 找name='abc'的列 如果有找到 exists返回真 否则 返回假 加上if语句 还可以取反操作 if not exists
1, select * from a where id in (select id from b) 2,select * from a where id in (select id from b) and phone is not null and phone <>‘’ 3,select * from a where id not in (select id from b) and phone is not null and phone <>''
对的。我顺便给你写下exists的对应语句: select * from a where exists(select 1 from b where a.id = b.id ) select * from a where exists(select 1 from b where a.id = b.id ) and phone is not null select * from a where not exists(select 1 from b where a.id = b.id ) and phone is not null
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
开心购99商家卖一件东西 给开心购99多少钱?
气态氯化钠里有分子存在吗?
临时身份证是什么样子,办理要多少钱,有效期
更年期心慌胸闷,是正常现象吗?
大话二人物洗点要多少大话币?
请教:启示与启发有什么不同之处?
天津哪里招工
小说《小兵传奇》的字数大概是多少?
在三角形ABC中,∠A=60度,点M为边AC的中点,
我是乡下户口,我在其他镇上农村买的房子算不
我的皮肤两颊毛孔大又明显、鼻子上有黑头、肤
这个是什么香蕉那么小?
现在抽烟一天要抽1包半,我才25岁怎么减少抽
相聚中国节妈妈等你回来观后感
东航白金卡年费可以免吗
推荐资讯
请把这个照片修成5寸
关于一个同学的提问“用Java编程判断键盘输入
尚美专业减肥怎么去啊,有知道地址的么
太阳系各大行星各自存在多久了?还将存在多久
天天易店可信吗?
蟑螂会相互传播信息吗?
蓝宝石主板990fx和蓝宝石970主板哪个好
科雷傲2012款怎么样?
现在济南大学,如果四级没有过,会影响学位证
美国商业银行资本充足率2011年是多少
普通轿车刮雨片一对多少钱?
百花艳婚庆礼仪策划地址在什么地方,想过去办
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?