返回信息流有两个表,分别表示用户的发图记录和发帖记录,写一SQL语句,求发帖数最少的用户发的图片数,以下是表结构,其中两个表以img_user,rev_user关联。
Img_id Img_user
1.jpg 2
2.gif 3
3.png 2
表image
Rev_id Rev_user
0 1
1 2
2 2
表revision
如:select _______ as user_id, _______ as rev_count, _______ as img_count from ______
高手请指教,多谢。
这是一条镜像帖。来源:北邮人论坛 / database / #3913同步于 2009/10/14
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Database机器人发帖
搜狐数据库SQL考题
liuge007
2009/10/14镜像同步5 回复
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
--oracle
select count(*)
from image
where img_user
in (
select t1.rev_user
from (
select rev_user,count(*) as rev_count
from revision
group by rev_user
order by rev_count asc) as t1
where rownum=1
)--发帖最少的者
)
--通用
select count(*)
from image
where img_user
in (
select t2.rev_user
from (select min(rev_count)
from (
select rev_user,count(*) as rev_count
from revision
group by rev_user
)
) as t1,
(select rev_user,count(*) as rev_count
from revision
group by rev_user
) as t2
where t1.rev_count=t2.rev_count
)
各横线的值
如:select _______ as user_id, _______ as rev_count, _______ as img_count from ______
第一个:
select t2.rev_user
from (select min(rev_count)
from (
select rev_user,count(*) as rev_count
from revision
group by rev_user
)
) as t1,
(select rev_user,count(*) as rev_count
from revision
group by rev_user
) as t2
where t1.rev_count=t2.rev_count
第二个:
select min(rev_count)
from (
select rev_user,count(*) as rev_count
from revision
group by rev_user
)
第三个:
select count(*)
from image
where img_user
in (
select t2.rev_user
from (select min(rev_count)
from (
select rev_user,count(*) as rev_count
from revision
group by rev_user
)
) as t1,
(select rev_user,count(*) as rev_count
from revision
group by rev_user
) as t2
where t1.rev_count=t2.rev_count
)
第四个:
dual
select count(*) from image
where img_user=(
select rev_user from
(select rev_user,count(*) as rev_count from revision group by rev_user) as t1
group by t1.rev_user
order by min(rev_count)
limit 1
);