sql 2表查询问题

发布于 2024-11-08 17:02:39 字数 608 浏览 0 评论 0原文

我的数据库中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦归所梦 2024-11-15 17:02:39

您没有指定您的 RDBMS,但假设它是 SQL Server:

select U.id, U.ad, COUNT(ISNULL(B.id, 0)) as 'total' 
from tblProducts U 
left join TblBasvurular B on B.urunid = U.id 
Group by u.id, u.ad

You didn't specify your RDBMS, but assuming it is SQL Server:

select U.id, U.ad, COUNT(ISNULL(B.id, 0)) as 'total' 
from tblProducts U 
left join TblBasvurular B on B.urunid = U.id 
Group by u.id, u.ad
青瓷清茶倾城歌 2024-11-15 17:02:39

这还不够清楚,因为你没有向我们展示第二张桌子。
但尝试在查询中将内连接更改为左连接:

select 
U.id,
U.ad,
COUNT(B.id) as 'total' 
from tblProducts U 
left join TblBasvurular B on B.urunid=U.id 
Group by u.id,u.ad

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:

select 
U.id,
U.ad,
COUNT(B.id) as 'total' 
from tblProducts U 
left join TblBasvurular B on B.urunid=U.id 
Group by u.id,u.ad
蓝海 2024-11-15 17:02:39

使用LEFT JOIN:

select U.id,U.ad,COUNT(B.id)as 'total' 
from tblProducts U LEFT join TblBasvurular B on B.urunid=U.id 
Group by u.id,u.ad

如果是Oracle,那么试试这个:

select U.id,U.ad,NVL(COUNT(B.id),0) as 'total' 
from tblProducts U LEFT join TblBasvurular B on B.urunid=U.id 
Group by u.id,u.ad

这将显示0而不是NULL。

如果是SQL Server,则使用ISNULL而不是NVL;如果是MySQL,则使用IFNULL。

对于所有这些 RDBMS,coalesce 函数都有效。

Use LEFT JOIN:

select U.id,U.ad,COUNT(B.id)as 'total' 
from tblProducts U LEFT join TblBasvurular B on B.urunid=U.id 
Group by u.id,u.ad

If it is Oracle, then try this:

select U.id,U.ad,NVL(COUNT(B.id),0) as 'total' 
from tblProducts U LEFT join TblBasvurular B on B.urunid=U.id 
Group by u.id,u.ad

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文