如果我使用查询:
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)
?
答案 0 :(得分:1)
我相信你的查询需要以GROUP BY结束,因为它包含一个聚合函数。
GROUP BY ei.ItemIdn, ei.UserIdn
答案 1 :(得分:0)
我认为你只是缺少GROUP BY子句 - http://sqlfiddle.com/#!2/24d19/26
SELECT
ei.ItemIdn as ItemIdn,
ei.UserIdn as UserIdn,
COUNT(cv.ItemIdn) as CountShow
FROM items ei
LEFT JOIN CountViewes as cv on ei.ItemIdn = cv.ItemIdn
WHERE ei.UserIdn = '0000'
GROUP BY ItemIdn, UserIdn
LIMIT 0, 25