返回mysql中具有重复列值的记录

时间:2011-11-10 18:07:04

标签: mysql sql

我正在尝试计算列中值的出现次数,并返回出现次数大于1的行。

例如:

SomeId   AnotherId
1        BC4
1        CDE
1        SDF
5        ER
5        WER
4        DFGR4

我希望能够获得列SomeId中出现次数大于1的SomeId

我看过使用SUMGROUP,但没有运气:(

7 个答案:

答案 0 :(得分:2)

这将返回多次出现的ID。

SELECT SomeId
FROM yourTable
GROUP BY SomeId
HAVING COUNT(1) > 1

答案 1 :(得分:1)

SELECT SomeId
    FROM YourTable
    GROUP BY SomeId
    HAVING COUNT(SomeId) > 1;

答案 2 :(得分:0)

SELECT SomeId, COUNT(SomeId) AS someidcount
FROM tablename
GROUP BY SomeId
HAVING someidcount > 1

答案 3 :(得分:0)

试试这个:

select count(*) as frequency, SomeId group by SomeId having frequency > 1

答案 4 :(得分:0)

您使用GROUP BY走在正确的轨道上,但您还需要HAVING。

试试这个:

SELECT SomeId, COUNT(SomeId)
FROM table
GROUP BY SomeId
HAVING COUNT(SomeId) > 1

以下是初学者a Wikipedia article on the HAVING clause的链接。

答案 5 :(得分:0)

试试这个:

select SomeId, Count(distinct AnotherId)
  from YourTable
 group by SomeId
having Count(distinct AnotherId) > 1

答案 6 :(得分:-1)

如果您熟悉PHP,可以这样做:

$sql = "SELECT * FROM table";
$res = mysql_query($sql);
while($row = mysql_fetch_assoc):
if($row['SomeId'] > 1):

{do whatever you like here...}

endif;
endwhile;