mysql - SQL查询问题
问题描述
表A 表Buser_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 BON A.user_id=B.user_id WHERE B.sorce=’’;这个SQL不知道对不对?1.表A中的user_id=3的王五,但王五的记录在B表中没有,我用左链接查询出来2.sorce =’’解决分数为空 查询出来
问题解答
回答1:不太同意一楼的sql语句,第一是因为子查询,第二好像没有检索出没有分数记录的。
SELECT * FROM product.a left join product.b on aid=aid2 where b.aid2 is null or sc is null;
这是我自己本地测试的,用的是mysql,但是其实都差不多。先用表连接把两个表按照规定的id链接在一起,aid对应是你a表里的userid,aid2对应是你b表的userid,然后最后上图。图一是我的sql语句的结果,图二是我的a表,图三是我的b表。
这也只是解决的一种方式,我也不是很擅长后台数据库~~希望对你有帮助,也希望有更好的解决方案。因为我没有考虑性能问题。
回答2:select * from 表A where user_id not in (select user_id from 表B where score >=0 )回答3:
select user_id, namefrom table_a where user_id not in (select user_id from table_b)union allselect b.user_id, a.namefrom table_b binner join table_a a on a.user_id = b.user_idwhere b.sorce=’’
有时候判断条件语句过于复杂可以使用union all将各个条件结果连接在一起.
相关文章:
1. 怎样用python给mp3添加封面?2. linux - 【已解决】fabric部署的Python项目Apache启动之后提示403Forbidden该如何解决?3. 上传多图时,最后一张图为缩略图,想设置第一张图为缩略图怎么解决?4. python - 如何获取rabbitmq连接的celery的个数5. js怎样实现在类似qq的联系人分组中进行搜索后,又可恢复原分组的界面?6. python 中文乱码问题7. 数据库 - Mysql的存储过程真的是个坑!求助下面的存储过程哪里错啦,实在是找不到哪里的问题了。8. javascript - 百度地图一开始设置了中心点,怎么通过按钮在调转到对应的中心点????9. mysql优化 - mysql 一张表如果不能确保字段列长度一致,是不是就不需要用到char。10. python - 如何将文本文件的CR换行符换成LF换行符?