假设我有一个表呼叫客户端。 在这张表中我有3个字段(A,B,C) 我希望在A = B = C的值时对每一行进行分组 例如:
A B C otherRow
1 2 3 x
2 1 x
4 2 y
我想得到以下内容
(A,B,C) otherRow Count
1 x 2
2 x 2
2 y 1
3 x 1
4 y 1
答案 0 :(得分:3)
您的查询为UNION
,而不是JOIN
:
SELECT
`A,B,C`,
otherRow,
COUNT(`A,B,C`) AS `Count`
FROM
(SELECT a AS `A,B,C`, otherRow FROM t
UNION ALL
SELECT b AS `A,B,C`, otherRow FROM t
UNION ALL
SELECT c AS `A,B,C`, otherRow FROM t) AS u
GROUP BY
`A,B,C`,
otherRow
HAVING
`A,B,C` IS NOT NULL
检查此fiddle。我添加了NULL
- 检查,因为不清楚你的“空”值是什么。如果您将其删除,您将获得零计数的NULL行。