我希望能够在不同的行ID上获得字段'count'的总和。
以下是我的查询。我们怎样才能做到这一点?
SELECT l.sid, l.Title, u.CompanyName, l.activation_date, l.expiration_date, COUNT(s.count), COUNT(DISTINCT a.id)
FROM listings l
LEFT JOIN statistics s
ON s.object_sid = l.sid
LEFT JOIN users u
ON u.sid = l.user_sid
LEFT JOIN applications a
ON a.listing_id = l.sid
WHERE l.sid = 6276
AND s.event = 'viewListing'
GROUP BY l.sid
LIMIT 10
答案 0 :(得分:0)
如果您需要列出statistics.count
列的“列表”和“应用”,那么您需要GROUP BY
“列表”和“应用”:
SELECT
l.sid, l.Title, l.activation_date, l.expiration_date,
u.CompanyName,
SUM(s.count), COUNT(a.id) -- <= SUM(s.count)
FROM listings l
LEFT JOIN statistics s ON s.object_sid = l.sid
LEFT JOIN users u ON u.sid = l.user_sid
LEFT JOIN applications a ON a.listing_id = l.sid
WHERE l.sid = 6276
AND s.event = 'viewListing'
GROUP BY l.sid, a.sid -- <= GROUP BY clause modified