select xx from diary where del=0 and datediff(d,updatetime,getdate())<20 and hf_id=0 其他不会写了...
谢谢
...全文
29418打赏收藏
求SQL语句
要求 表diary 1.选出top 20 人 2.根据最近30天发布日记的数量排行 datediff(d,diary_timed,getdate())<30 3.要求显示每个人的日记数量 4.显示姓名 userid 5.显示标题 title 现在等 ..马上揭帖! select xx from diary where del=0 and datediff(d,updatetime,getdate())<20 and hf_id=0 其他不会写了... 谢谢
select a.userid,a.title,b.日记数量
from diary a inner
(
select top 20 userid,日记数量=count(title) from diary where del=0 and datediff(d,updatetime,getdate())<20 and hf_id=0 group by userid
)
b
on a.userid=b.userid
select top 20 userid,title,count(distinct title) as 日记数量
from diary
where del=0 and datediff(d,updatetime,getdate())<20 and hf_id=0
group by userid,title
select top 20 userid,title,count(title) as 日记数量
from
(
select * from diary a where not exists(select 1 from diary where userid=a.userid and title=a.title and updatetime>a.updatetime)
)
b
where del=0 and datediff(d,updatetime,getdate())<20 and hf_id=0
group by userid,title
select distinct userid,title,数量
from diary a
inner join
(
select top 20
userid,
(select count(title) from diary where datediff(d,diary_timed,getdate())<30 group by userid) as 数量
from diary c
order by 数量 desc
)b
on a.userid=b.userid
select distinct userid,title,数量
from diary a
inner join
(
select top 20
userid,
(select count(title) from diary where datediff(d,diary_timed,getdate())<30) group by userid) as 数量
from diary
order by 数量
desc
)b
on a.userid=b.userid
select distinct userid,title,数量
from diary a
inner join
(
select top 20
userid,
(select count(userid) from diary where datediff(d,diary_timed,getdate())<30) group by userid) as 数量
from diary
order by 数量
desc
)b
on a.userid=b.userid
select distinct top 20 userid,title,count(title) as 日记数量
from diary
where del=0 and datediff(d,updatetime,getdate())<20 and hf_id=0
group by userid,title
or
select top 20 * from
(
select distinct userid,title,count(title) as 日记数量
from diary
where del=0 and datediff(d,updatetime,getdate())<20 and hf_id=0
group by userid,title
) b