`
backbase
  • 浏览: 89195 次
  • 性别: Icon_minigender_1
  • 来自: 虾汤水库
社区版块
存档分类
最新评论

sql练习 -----求出发帖最多的人

    博客分类:
  • db
阅读更多
   昨天无意识看到同桌的同事在练习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)//应该加上括号
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics