sql 2表查询问题
我的数据库中有 2 个表。我使用以下查询:
Select U.id, U.ad, COUNT(B.id) AS 'total'
FROM tblProducts U
INNER JOIN TblBasvurular B ON B.urunid=U.id
GROUP BY u.id,u.ad
这显示了我
id | productname | total
-------------------------------
1 | bread | 2
2 | water | 3
3 | milk | 1
,但我看不到其他产品。如果不存在,它不会向我显示其他内容。
我怎样才能得到这样的结果:
id | productname | total
-------------------------------
1 | egg | 0
2 | bread | 2
3 | water | 3
4 | tea | 0
5 | milk | 1
I have 2 tables in a database. I use following query:
Select U.id, U.ad, COUNT(B.id) AS 'total'
FROM tblProducts U
INNER JOIN TblBasvurular B ON B.urunid=U.id
GROUP BY u.id,u.ad
this show me
id | productname | total
-------------------------------
1 | bread | 2
2 | water | 3
3 | milk | 1
but I can't see other products. If does not exist it does not show me others.
How can I get result like this:
id | productname | total
-------------------------------
1 | egg | 0
2 | bread | 2
3 | water | 3
4 | tea | 0
5 | milk | 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有指定您的 RDBMS,但假设它是 SQL Server:
You didn't specify your RDBMS, but assuming it is SQL Server:
这还不够清楚,因为你没有向我们展示第二张桌子。
但尝试在查询中将内连接更改为左连接:
It is not clear enough, because you didn't show us second table.
But try to change inner join to left join in your query:
使用LEFT JOIN:
如果是Oracle,那么试试这个:
这将显示0而不是NULL。
如果是SQL Server,则使用ISNULL而不是NVL;如果是MySQL,则使用IFNULL。
对于所有这些 RDBMS,
coalesce
函数都有效。Use LEFT JOIN:
If it is Oracle, then try this:
This will display 0 instead of NULL.
If it is SQL Server, use ISNULL instead of NVL; if it is MySQL, use IFNULL.
For all of these RDBMSs, the function
coalesce
works.