mysql - SQL查询问题
PHP中文网
PHP中文网 2017-04-17 15:30:26
[MySQL讨论组]
表A                      表B
user_id   name           id    user_id   sorce
 1        张三            1     1         78
 2        李四            2     2         
 3        王五            3     5         80
 
想要查询表A中 没有分数或者表B中没有分数记录的,sql如何写?

select A.user_idid A.name FROM A LEFI JOIN B
ON A.user_id=B.user_id WHERE B.sorce='';
这个SQL不知道对不对?
1.表A中的user_id=3的王五,但王五的记录在B表中没有,我用左链接查询出来
2.sorce =''解决分数为空 查询出来
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(2)
大家讲道理
select * from  表A where user_id not in (select user_id from 表B where score >=0 )
ringa_lee
select user_id, name
from table_a 
where user_id not in (select user_id from table_b)
union all
select b.user_id, a.name
from table_b b
inner join table_a a on a.user_id = b.user_id
where b.sorce=''

有时候判断条件语句过于复杂可以使用union all将各个条件结果连接在一起.

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号