sql練習 -----求出發帖最多的人
昨天無意識看到同桌的同事在練習sql,其中看到他在寫一道題的sql,
求出發帖最多的人?
開始我寫的是這句:
select t.authorid,max(total) from (select authorid,count(*) total from articles
group by authorid ) t group by t.authorid 結果去執行,完了,錯的! 寫了兩句相當於
寫了一句,都是一樣的結果。
正確的寫法如下:
select teaid,count(*) total from articles group by teaid having count(*) =
(select max(total2) from (select count(*) total2 from articles group by teaid) t);
記得不能寫成如下這樣哦:
select teaid,count(*) total from articles group by teaid
having total = //total 一定要寫成 count(*)
(select max(total2) from (select count(*) total2 from articles group by teaid) t);
下面這樣寫直接報錯
select teaid,count(*) total from articles group by teaid having
count(*)=max(total)
也不應該寫成:
select teaid,count(*) total from articles group by teaid
having total = //total 一定要寫成 count(*)
(select max(total2) from (select count(*) total2 from articles group by teaid)
as t);//子查詢不能加as as只能用作表的別名
下面的寫法也會報錯
select teaid , count(*) c from articles group by teaid
having count(*) = select max(t.total) from (select teaid,count(*) total from articles group by teaid) t
應該寫成下面這樣
select teaid , count(*) c from articles group by teaid
having count(*) = (select max(t.total) from (select teaid,count(*) total from
articles group by teaid) t)//應該加上括號
求出發帖最多的人?
開始我寫的是這句:
select t.authorid,max(total) from (select authorid,count(*) total from articles
group by authorid ) t group by t.authorid 結果去執行,完了,錯的! 寫了兩句相當於
寫了一句,都是一樣的結果。
正確的寫法如下:
select teaid,count(*) total from articles group by teaid having count(*) =
(select max(total2) from (select count(*) total2 from articles group by teaid) t);
記得不能寫成如下這樣哦:
select teaid,count(*) total from articles group by teaid
having total = //total 一定要寫成 count(*)
(select max(total2) from (select count(*) total2 from articles group by teaid) t);
下面這樣寫直接報錯
select teaid,count(*) total from articles group by teaid having
count(*)=max(total)
也不應該寫成:
select teaid,count(*) total from articles group by teaid
having total = //total 一定要寫成 count(*)
(select max(total2) from (select count(*) total2 from articles group by teaid)
as t);//子查詢不能加as as只能用作表的別名
下面的寫法也會報錯
select teaid , count(*) c from articles group by teaid
having count(*) = select max(t.total) from (select teaid,count(*) total from articles group by teaid) t
應該寫成下面這樣
select teaid , count(*) c from articles group by teaid
having count(*) = (select max(t.total) from (select teaid,count(*) total from
articles group by teaid) t)//應該加上括號
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.