这个问题是很早的时候别人给我的,当时没答出来,后来就淡忘了。今天在几个群的热烈气氛下居然重提出来了。下面是数据库
id name course
6 d chinese
5 c english
3 a math
4 b english
7 c math
8 e chinese
9 a english
10 e sport
11 c sport
12 e english
15 b sport
14 g chinese
使用mysql语句:
SELECT * FROM tb WHERE name IN (SELECT name FROM tb GROUP BY name HAVING count( course ) >=2) ORDER BY name LIMIT 0 , 30;
第二种写法(由小芸提供)
select t.* from tb t where (select count(*) from tb where name = t.name)>=2 order by t.name LIMIT 0,30;
得到结果:
id name course
9 a english
3 a math
15 b sport
4 b english
7 c math
11 c sport
5 c english
8 e chinese
10 e sport
12 e english
id name course
6 d chinese
5 c english
3 a math
4 b english
7 c math
8 e chinese
9 a english
10 e sport
11 c sport
12 e english
15 b sport
14 g chinese
使用mysql语句:
SELECT * FROM tb WHERE name IN (SELECT name FROM tb GROUP BY name HAVING count( course ) >=2) ORDER BY name LIMIT 0 , 30;
第二种写法(由小芸提供)
select t.* from tb t where (select count(*) from tb where name = t.name)>=2 order by t.name LIMIT 0,30;
得到结果:
id name course
9 a english
3 a math
15 b sport
4 b english
7 c math
11 c sport
5 c english
8 e chinese
10 e sport
12 e english