mysql面试题

有一张product表如下,请查询每个城市买的最好的商品的good_id.
在这里插入图片描述

select city,good_id
  from
	(select  city,good_id,cnt,max(cnt) over(PARTITION by city order by cnt desc ) as maxcnt
			from 
				(select distinct city, good_id, count(good_id) over(PARTITION by city,good_id  ) as cnt
					from product) as t
	) as l where l.cnt=l.maxcnt	
select a.good_id,a.city,t.cnt from 
  (select good_id,city,count(good_id) as cnt from product GROUP BY good_id,city) as a 
	  join
		 (select city,max(cnt) as cnt from 
			 (select good_id,city,count(good_id) as cnt from product GROUP BY good_id,city) as b group by city ) as t
	on a.cnt = t.cnt and a.city = t.city

知识点:
在这里插入图片描述

创建如下表,找up主平均上传视频时间大于300的video_id,且按照平均时间降序,video_id升序,返回video_id的3-6行

CREATE TABLE uploader_video_record (
    video_id BIGINT PRIMARY KEY,
    uploader_id BIGINT,
    video_duration int
);

INSERT INTO uploader_video_record (video_id, uploader_id, video_duration)
VALUES
 (1, 101, 60),
(2, 102, 600),
(3, 103, 310),
(4, 101, 120),
(5, 104, 3200),
(6, 102, 330),
(7, 103, 290),
(8, 105, 290),
(9, 101, 180),
(10, 103, 320);

答案:

select video_id from uploader_video_record as a join
(select uploader_id,duration from 
		(select uploader_id,avg(video_duration) as duration from uploader_video_record GROUP BY uploader_id ) as t
		where t.duration>300) as b 
		on a.uploader_id=b.uploader_id order by b.duration desc, a.video_id asc limit 2,4;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值