我有很大的桌子,我们称之为MyTable。
让它看起来像这样:
guid,container,container_slot,something,something_2,something_3
我想获得重复的选择结果,但是使用3列检查。
让我以更方便的方式解释:
guid, container, container_slot, something, something_2, something_3
1, 1, 1, 5, 2, 1
1, 1, 2, 63, 5, 2 (!)
1, 1, 2, 12, 2, 63 (!)
1, 1, 3, 1, 2, 63
1, 2, 5, 64, 2, 63
2, 1, 1, 34, 8, 21
2, 1, 3, 2, 1, 25
这里的一切都没问题,除了两条记录,最后是(!)的记录。
在普通表中,我将guid,container和container_slot设置为主键,以防止那些(!)行。
但是这张桌子很大,而且会让它变慢。
我想选择这两行,其中:
guid, containter, container_slot
在同一时刻重复,所有其他插槽可以是任何一个。
这样:
guid, container, containter_slot
1, 1, 2
1, 1, 3
没关系,但是:
1, 1, 2
1, 1, 2
重复。
有没有办法给我这样的结果? :
guid, container, container_slot, something, something_2, something_3
1, 1, 2, 63, 5, 2
1, 1, 2, 12, 2, 63
答案 0 :(得分:0)
select guid, container, container_slot
from mytable
group by guid, container, container_slot
having count(1) > 1
这将通过其键给出所有重复的行。将其转换为子查询和内部联接以充当过滤器
select mytable.* from
(select guid, container, container_slot
from mytable
group by guid, container, container_slot
having count(1) > 1)a
inner join mytable on mytable.guid = a.guid and mytable.container = a.container and mytable.containerslot = a.containerslot
where in / where where查询的工作原理... mysql处理不当并增加运行时间,所以我倾向于使用内部连接方法