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)//应该加上括号
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值