DISTINCT行的字段的和

时间:2013-05-02 07:51:08

标签: mysql

我希望能够在不同的行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

1 个答案:

答案 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