我有一个SQL表数据如下。

c_type_id | s_id | value
     1    | 11   | Winx32
     1    | 12   | Domain1
     2    | 11   | Winx64
     2    | 12   | Domain2
     3    | 11   | Winx32
     3    | 12   | Domain1
     4    | 11   | Winx32
     4    | 12   | Domain2


如何查询和分组以获得以下结果?

countall | platform | domain
       2 | Winx32   | Domain1
       1 | Winx64   | Domain2
       1 | Winx32   | Domain2

最佳答案

SELECT  COUNT(*) countall,
        p.value platform,
        d.value domain
FROM    mytable p
JOIN    mytable d
ON      (d.c_type_id, d.s_id) = (p.c_type_id, 12)
WHERE   p.s_id = 11
GROUP BY
        d.c_type_id

07-24 14:36