學科大於2的學生,按姓名排序資料

這個問題是很早的時候別人給我的,當時沒答出來,後來就淡忘了。今天在幾個羣的熱烈氣氛下居然重提出來了。下面是數據庫
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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章