0

我有一个MYSQL数据库,我想根据列中的两个值比较重复值。例如

数据库

field_one | field_two | field_three
----------------------------------
啊!123 | 1号
bbb | 第456章 1号
啊!123 | 2号
抄送 | 第456章 3号
啊!123 | 3号

我想返回这些结果

field_one | field_two | field_three
----------------------------------
啊!123 | 1号
啊!123 | 2号

这是query我一直在使用的,但我不确定如何获得我想要的结果。当我queryphpMyAdmin浏览器中运行时会卡住。我的数据库也很大。

SELECT * FROM table_name WHERE field_three IN (SELECT field_one, field_two FROM table_name WHERE field_three IN ('no1', 'no2') HAVING COUNT(*) > 1)

谢谢


解决了

我刚刚将WHERE@Gordon Linoff 从query.

select t.*
from table_name t join
     (select field_one, field_two
      from table_name t
      group by field_one, field_two
      having count(*) = 2   -- or do you mean >= 2?
     ) tsum
     on t.field_one = tsum.field_one and t.field_two = tsum.field_two WHERE field3 IN ('no1', 'no2')
4

1 回答 1

0

我想你想要这个:

select t.*
from t join
     (select field_one, field_two
      from t
      group by field_one, field_two
      having count(*) = 2   -- or do you mean >= 2?
     ) tsum
     on t.field_one = tsum.field_one and t.field_two = tsum.field_two

子查询根据前两列查找重复项。外部查询返回原始行。

如果您想要相对于另一个值的重复项,则将该条件添加到子查询中:

select t.*
from t join
     (select field_one, field_two
      from t
      where field3 = 'no1' -- or whatever you want to set it to
      group by field_one, field_two
      having count(*) = 2   -- or do you mean >= 2?
     ) tsum
     on t.field_one = tsum.field_one and t.field_two = tsum.field_two
于 2013-04-19T20:36:09.860 回答