从mysql中选择重复值并显示所有值

时间:2014-12-09 17:25:19

标签: mysql sql

我目前有以下SQL查询来提取所有重复值:

Select `Racetime`,`Trainer`
from tom_cards
where `Trainer` in (
  select `Trainer`
  FROM tom_cards
  GROUP BY `Racetime`,`Trainer`
  HAVING COUNT(*) > 1
)

我希望有更像

的东西
Select `Racetime`,`Trainer`
from tom_cards
where `Trainer` and `Racetime in (
  select `Trainer` and `Racetime`
  FROM tom_cards
  GROUP BY `Racetime`,`Trainer`
  HAVING COUNT(*) > 1
)

但这无效。什么是有效的查询来表达同样的事情?

3 个答案:

答案 0 :(得分:0)

有时我通过在子查询中执行group by子句然后从中选择我想要的行来处理这些情况。

SELECT Racetime, Trainer FROM (
  SELECT Racetime, Trainer, COUNT(*) as occurrences 
  FROM tom_cards
  GROUP BY 1,2
) Racetime_Trainer_counts
WHERE occurrences > 1

答案 1 :(得分:0)

你可以连接

Select Racetime,Trainer from tom_cards 
where concat(Racetime,Trainer) in 
    (select concat(Racetime,Trainer) FROM tom_cards 
         GROUP BY Racetime,Trainer 
           HAVING COUNT(*) > 1)

答案 2 :(得分:0)

您可以加入子查询结果:

SELECT `Racetime`,`Trainer`
FROM tom_cards
  JOIN (
    SELECT `Racetime`,`Trainer`
    FROM tom_cards
    GROUP BY `Racetime`,`Trainer`
    HAVING COUNT(*) > 1
  ) dupes
    ON tom_cards.Racetime = dupes.Racetime AND tom_cards.Trainer = dupes.Trainer