gpt4 book ai didi

mysql - 如何从总记录中获取不存在的记录为空?

转载 作者:太空宇宙 更新时间:2023-11-03 10:38:01 24 4
gpt4 key购买 nike

表 1:tbl_users:

+----+--------+
| id | name |
+----+--------+
| 1 | waheed |
+----+--------+
| 2 | fareed |
+----+--------+

表 2:tbl_watched:

+------------+----+--------+
| id_watched | id | name |
+------------+----+--------+
| 1 | 2 | fareed |
+------------+----+--------+

如果 tbl_watched 不存在,我想获取总记录,它应该返回为 zeronull

输出:

+----+--------+--------+
| id | name |watched |
+----+--------+--------+
| 1 | waheed | 90 |
+----+--------+--------+
| 2 | fareed | null |
+----+--------+--------+

我怎样才能得到这样的结果?

最佳答案

使用左连接

Select t1.id , t1.name, count(*) as total from tbl_users as t1 
left join tbl_watched as t2 on t1.id=t2.id_watched
group by t1.id , t1.name

关于mysql - 如何从总记录中获取不存在的记录为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44154019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com