不同于MySQL中的多个列

时间:2022-08-24 21:13:01

I wish to find out the count of distinct rows in a MySQL DB.

我希望找到MySQL DB中不同行的计数。

id | val1 | val2 | val3

1  |  1   |  1   |  1
2  |  1   |  1   |  1
3  |  2   |  2   |  2
4  |  2   |  2   |  2

On the table above the query would return

在上面的表中,查询将返回

val1 | val2 | val3 | count
1    |   1  |  1   |   2 
2    |   2  |  2   |   2

Does anyone know a reasonably efficient way of achieving this in MySQL.

有人知道在MySQL中实现这一目标的一种合理有效的方法吗?

1 个解决方案

#1


5  

You need to use GROUP BY clause for this:

您需要使用GROUP BY子句:

SELECT val1, val2, val3, count(*) AS count
FROM mytable GROUP BY val1, val2, val3

See this fiddle

#1


5  

You need to use GROUP BY clause for this:

您需要使用GROUP BY子句:

SELECT val1, val2, val3, count(*) AS count
FROM mytable GROUP BY val1, val2, val3

See this fiddle