数不喜欢和点击

时间:2013-08-31 09:31:08

标签: mysql count group-by

您好我有4张这样的表。

enter image description here

如何sql sintax得到那样的?

之前谢谢。 :d

1 个答案:

答案 0 :(得分:1)

下面的查询将列出Computer表中的所有记录是否与其他表匹配id,因为使用了LEFT JOIN。单独计算计算机点击次数的原因是,如果表Computer中存在LIKE的多个匹配项,则可能会产生无效计数。

SELECT  a.ID, a.Name, 
        SUM(sta_like = 1) total_like,
        SUM(sta_like = 0) total_dislike,
        COALESCE(totalHits, 0) total_hits
FROM    Computer a
        LEFT JOIN `LIKE` b
            ON a.id = b.id_com
        LEFT JOIN
        (
            SELECT  id_com_hits, COUNT(*) totalHits
            FROM    hits
            GROUP   BY id_com_hits
        ) c ON a.ID = c.id_com_hits
GROUP   BY a.ID, a.Name