亲历sql题分享

https://www.bilibili.com/video/BV1PU4y1p7jP?p=1
https://www.jianshu.com/p/0f165dcf9525

  • sql查询分组并将一列按多行展示


    原表

要查成


select year,sum(m1) m1,sum(m2) m2,sum(m3) m3,sum(m4) m4 from (
SELECT year,
case when month=1 then amount end m1,
case when month=2 then amount end m2,
case when month=3 then amount end m3,
case when month=4 then amount end m4
FROM `test1` 
)
tmp group by year;

利用Max(CASE ... WHEN ... THEN)语法来使其转为行:

SELECT year,
max(case month when '1' then amount end) m1,
max(case month when '2' then amount end) m2,
max(case month when '3' then amount end) m3,
max(case month when '4' then amount end) m4
from test1 
group by year;

1、

select u.user_id,sex,age,count_7 ,count_14
from user_info u, 
(
select user_id,count(order_id) count_7 from 
sale_order where creat_time <=date_add('1997-01-12',interval 7 day ) 
and 
creat_time >=date_add('1997-01-12',interval 0 day ) 
group by user_id) m,
(
select user_id,count(order_id) count_14 from 
sale_order where creat_time <=date_add('1997-01-12',interval 14 day ) 
and 
creat_time >=date_add('1997-01-12',interval 0 day ) 
group by user_id) n 
where u.user_id =m.user_id and m.user_id=n.user_id and u.user_id = n.user_id;

2、

#复制sale_order表为sale_order_copy1 
update sale_order_copy1 a1,sale_order_inc a2 set a1.user_id=a2.user_id,a1.creat_time = a2.creat_time  
where a1.order_id =a2.order_id;

insert into sale_order_copy1(order_id,user_id,creat_time) 
select order_id,user_id,creat_time from sale_order_inc where sale_order_inc.order_id 
not in (select order_id from sale_order_copy1);

3、

#每个用户的总订单量
SELECT user_id,count(user_id) 总单量 FROM `sale_order` group by user_id order by 总单量 ;
#全部用户人数的中位数、上下四分位数
select round(count(distinct user_id)/4) 下四分位数,
round(count(distinct user_id)/2) as 订单量中位数, 
round(count(distinct user_id)/4*3) as 上四分位数 
from sale_order ;
#用户订单量上四分位数
select 总单量 from  (SELECT user_id,count(user_id) 总单量 FROM `sale_order` group by user_id) as user_orders  order by 总单量 limit 5893,1;
#用户订单量中位数
select 总单量 from  (SELECT user_id,count(user_id) 总单量 FROM `sale_order` group by user_id) as user_orders  order by 总单量 limit 11785,1;
#用户订单量下四分位数
select 总单量 from  (SELECT user_id,count(user_id) 总单量 FROM `sale_order` group by user_id) as user_orders  order by 总单量 limit 17678,1;

学生信息查询类

insert into student select 1,'刘一',18,'男' union all 
select 2,'钱二',19,'女' union all 
SELECT 4,'李四',18,'女' union all 
select 5,'王五' ,17,'男' union all 
select 6,'赵六' ,19,'女' ;

alter table teacher convert to character set utf8mb4;

insert into course select 1,'语文',1 union all
select 2,'数学',2 union all
select 3,'英语',3 union all
select 4,'物理',4;

INSERT into score
select 1,1,56 union all
select 1,2,78 union all
select 1,3,67 union all
select 1,4,58 union all
select 2,1,79 union all
SELECT 2,2,81 union all
select 2,3,92 union all
select 2,4,68 union all
select 3,1,91 union all
select 3,2,47 union all
select 3,3,88 union all
select 3,4,56 union all
select 4,2,88 union all
select 4,3,90 union all
select 4,4,93 union all
select 5,1,46 union all
select 5,3,78 union all
select 5,4,53 union all
select 6,1,35 union all
select 6,2,68 union all
SELECT 6,4,71

insert into teacher 
select 1,'叶平' union all
select 2,'贺高' union all
select 3,'杨艳' union all
select 4,'周磊'

1、查询语文成绩大于数学成绩的学生学号。

select a.sid from (select sid,score from score  where cid='1') a ,
(select sid,score from score where cid='2' ) b where a.score > b.score and a.sid=b.sid;

2、查询平均成绩大于60份的同学的学号和平均成绩

select sid,avg(score) from score group by sid having avg(score) >60;

3、查询所有同学的学号、姓名、选课数、总成绩;

select student.sid,student.sname,count(student.sid),sum(score.score) from student left join score on student.sid = score.sid group by student.sid,student.sname;

4、查询理姓李的老师的个数;

select count(tname)  姓李的老师个数 from teacher where tname like '%李%';

5、查询每门课程被选修的学生数;

SELECT cid,count(*) FROM score group by cid ;

6、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;

SELECT cid,avg(score) FROM score group by cid order by avg(score) asc,cid desc;

7、查询所有课程成绩小于60分的同学的学号、姓名;

#即排除掉只要有课程分数大于60的学生
select sid,sname from student where sid not in (select distinct sid from score where score >60 );

8、删除学习“叶平”老师课的sc表记录;

select * from score where cid in 
(select cid from course where tid in 
(select tid from teacher where tname='叶平'));
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,402评论 6 499
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,377评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,483评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,165评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,176评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,146评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,032评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,896评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,311评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,536评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,696评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,413评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,008评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,815评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,698评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,592评论 2 353

推荐阅读更多精彩内容