有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
对吗?
SQL查询语句in和exists 怎么查这两个表
答案:4 悬赏:70
解决时间 2021-03-14 11:38
- 提问者网友:不懂我就别说我变
- 2021-03-13 11:11
最佳答案
- 二级知识专家网友:统治我的世界
- 2021-03-13 12:48
基本对的,但是要注意下,null和‘’是有区别的,一般不确定是null还是‘’的时候,可以用and。
phone is not null and phone <> ''
另外,in相当于执行子查询后使用or对比,如果子查询数据量大时,用exists会快一些
phone is not null and phone <> ''
另外,in相当于执行子查询后使用or对比,如果子查询数据量大时,用exists会快一些
全部回答
- 1楼网友:不羁的心
- 2021-03-13 14:27
简单点说
if exists(select* from tb where name='abc') 就是这样用
上面这句话的意思是 查询表tb 找name='abc'的列
如果有找到 exists返回真 否则 返回假
加上if语句 还可以取反操作 if not exists
- 2楼网友:夢想黑洞
- 2021-03-13 13:52
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 <>''
- 3楼网友:不羁的心
- 2021-03-13 13:31
对的。我顺便给你写下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
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯