0

我希望能够在不同的行 ID 上获得字段“计数”的总和。

下面是我的查询。我们怎样才能做到这一点?

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
4

1 回答 1

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
于 2013-05-02T08:11:27.000 回答