gpt4 book ai didi

mysql - 如何通过左连接正确获取计数?

转载 作者:行者123 更新时间:2023-11-29 13:21:09 24 4
gpt4 key购买 nike

sqlfiddle上的结构表

如果我使用查询:

SELECT 
ei.ItemIdn as ItemIdn,
ei.UserIdn as UserIdn

FROM items ei

WHERE ei.UserIdn = '0000'
LIMIT 0, 25

我得到 4 行,但如果我想获取每行的观看者数:

SELECT 
ei.ItemIdn as ItemIdn,
ei.UserIdn as UserIdn,
ifnull(count(cv.ItemIdn),0) as CountShow

FROM items ei

left join CountViewes as cv on ei.ItemIdn = cv.ItemIdn

WHERE ei.UserIdn = '0000'
LIMIT 0, 25

我只得到一行。

请告诉我如何获取所有行和每行的 count(cv.ItemIdn)

最佳答案

我确实相信您的查询需要以 GROUP BY 结尾,因为它包含聚合函数。

GROUP BY ei.ItemIdn, ei.UserIdn

请参见此处:http://sqlfiddle.com/#!2/24d19/32/0

关于mysql - 如何通过左连接正确获取计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20829488/

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