假设我有一个表调用客户端。
在此表中,我有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

最佳答案

您的查询是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 -check,因为不清楚您的“空”值是多少。如果将其删除,则会得到零计数的NULL行。

关于mysql - MySQL组字段及其值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24387737/

10-12 01:30