这听起来像你想使用类似于此:
select id
from yourtable
group by `group`
having count(`group`) = 1
见SQL Fiddle with Demo。
然后,如果你想返回所有细节,那么你就可以展开查询:
select *
from yourtable t1
where id in (select id
from yourtable t2
group by `group`
having count(`group`) = 1)
见SQL Fiddle with Demo。
如果你想返回有同一组中的所有行,那么你可以使用:
select *
from yourtable t1
where `group` in (select `group`
from yourtable t2
group by `group`
having count(`group`) > 1)
见SQL Fiddle with Demo
然后,如果你想回到一切,标识标志,如果它是一个或多个,那么你可以使用类似的东西。你会发现,我包括一个标志,显示什么groups
有一个行,然后哪些群体有超过一排:
select *, 'single' Total
from yourtable t1
where `group` in (select `group`
from yourtable t2
group by `group`
having count(`group`) = 1)
union all
select *, 'multiple' Total
from yourtable t1
where `group` in (select `group`
from yourtable t2
group by `group`
having count(`group`) > 1)
见SQL Fiddle with Demo
好,它的工作,但其只获得了单打,如何得到id 1和2?不管怎样,谢谢。 – Centurion 2013-02-21 14:22:43
@Centurion。 。 。你将'having'从'= 1'改为'in(1,2)'。 – 2013-02-21 14:25:29
@戈登·利诺夫,这个想法是让这些有组超过了两个ID,我希望你明白 – Centurion 2013-02-21 14:30:43