如何计算两列中具有相同值的行,并仅返回count计数的行

时间:2012-08-15 09:45:33

标签: mysql sql

关于这个问题: How to count rows that have the same values in two columns (SQL)?

有没有办法只返回计数为<的行。 3?

+-----+-----+-----+
|  A  |  B  |count|
+=====+=====+=====+
|  1  |  3  |  2  |
+-----+-----+-----+
|  4  |  2  |  1  |
+-----+-----+-----+

2 个答案:

答案 0 :(得分:1)

HAVING子句

 SELECT ...
 FROM ...
 WHERE ...
 GROUP BY whatever
 HAVING count(*) <3

答案 1 :(得分:1)

只需在HAVING子句中添加条件

即可
SELECT colName, COUNT(*)
FROM tableName
GROUP BY colName
HAVING COUNT(*) < 3