选择不同的对mysql

时间:2012-08-17 22:18:11

标签: mysql distinct

我想选择跨A列和B列具有相同值的行。

例如,如果我的表是

A  B

1  2
3  4
1  2
4  5

输出应为

A  B
1  2

一个。 Select Distinct A,B from table

选择表格中的所有值。

B中。 Select Distinct (A,B) from table

告诉我,Distinct功能只能使用一个值。

℃。 Select A,B from table group by A,B

选择表格中的所有值(类似于A)。

问题类似于Selecting Distinct combinations. 但答案表明没有用。

1 个答案:

答案 0 :(得分:11)

您只想要具有重复项的行。您可以使用HAVING子句根据聚合函数过滤数据的“分组”:

SELECT   A,B
FROM     tbl
GROUP BY A,B
HAVING   COUNT(*) > 1