当前位置: 代码迷 >> 综合 >> 10系统函数面试题
  详细解决方案

10系统函数面试题

热度:33   发布时间:2024-02-28 13:01:24.0

系统函数

 流程控制的函数
-- 1> if(expr1,expr2,expr3)  
-- 第1个表达式为true则输出第2个表达式,否则输出第3个表达式

-- 高考成绩>=600分,则输出优秀,否则输出hehe

   select  if(sscore>=600,'优秀','hehe')  from t_student;


-- 2> ifnull(expr1,expr2) 如果expr1为null,则返回第2个表达式

-- 3> case when then  end 类似于java的switch
   case 值 
         when  条件1  then  输出结果1
         when  条件2  then  输出结果2
   else
       输出结果
   end

-- 【面试题】
 create table tb_stu(
         sno int primary key,
         sname varchar(10) 
     );
     create table tb_couse(
        cno char(4) primary key,
        cname varchar(10)
     );
     create table tb_sc(
        s_no int,
        c_no char(4),
        grade float(4,1),
        primary key(s_no,c_no)
     );
     alter table tb_sc add constraint fk_sno foreign key(s_no) references tb_stu(sno);
     alter table tb_sc add constraint fk_cno foreign key(c_no) references tb_couse(cno);
   
insert into tb_stu(sno,sname) values
(1101,'张三'),(1102,'李四'),(1103,'王五'),(1104,'赵六');

insert into tb_couse(cno,cname) values 
('A01','语文'),('A02','数学'),('A03','英语'),('A04','物理');

insert into tb_sc(c_no,s_no,grade) values( 'A01', 1101, 90);
insert into tb_sc(c_no,s_no,grade) values( 'A01', 1102, 70);
insert into tb_sc(c_no,s_no,grade) values( 'A01', 1103, 88);
insert into tb_sc(c_no,s_no,grade) values( 'A01', 1104, 95);


insert into tb_sc(c_no,s_no,grade) values( 'A02', 1101, 89);
insert into tb_sc(c_no,s_no,grade) values( 'A02', 1102, 90);
insert into tb_sc(c_no,s_no,grade) values( 'A02', 1103, 95);
insert into tb_sc(c_no,s_no,grade) values( 'A02', 1104, 20);


insert into tb_sc(c_no,s_no,grade) values( 'A03', 1101, 40);
insert into tb_sc(c_no,s_no,grade) values( 'A03', 1102, 80);
insert into tb_sc(c_no,s_no,grade) values( 'A03', 1103, 78);
insert into tb_sc(c_no,s_no,grade) values( 'A03', 1104, 76);

insert into tb_sc(c_no,s_no,grade) values( 'A04', 1101, 56);
insert into tb_sc(c_no,s_no,grade) values( 'A04', 1102, 89);
insert into tb_sc(c_no,s_no,grade) values( 'A04', 1103, 67);
insert into tb_sc(c_no,s_no,grade) values( 'A04', 1104, 55);

select * from tb_stu;
select * from tb_couse;
select * from tb_sc;


      查询结果为 (行转列)
      姓名    语文    数学   英语   物理
      张三    90       89     40    56
      李四    70       90     80    89
      王五    88       95     78    67
      赵六    95       20     76    55


  -- 第1步: 分析发现需要查询出 姓名,课程,分数
  -- 姓名来至于 学生表   课程来至于 课程表   分数 来至于 成绩表
  -- 3个表等值连接查询

    select  sname,cname,grade from tb_stu s, tb_couse c,tb_sc sc where s.sno=sc.s_no and c.cno=sc.c_no;


   -- 第2步:排序(姓名)

    select  sname,cname,grade from tb_stu s, tb_couse c,tb_sc sc where s.sno=sc.s_no and c.cno=sc.c_no order by sname;

    -- 第3步:分组(姓名)  -- group_concat 分组连接
    
    select  sname,group_concat(cname),group_concat(grade) from tb_stu s, tb_couse c,tb_sc sc where s.sno=sc.s_no and c.cno=sc.c_no
    group by sname
    order by sname;
  

   -- 第4步: case when 条件  then 输出结果  else 默认值 end 语法帮助我们行转列

    select 
           temp.sname 姓名,
           max(case when temp.cname='语文' then grade else 0 end) 语文,
           max(case when temp.cname='数学' then grade else 0 end) 数学,
           max(case when temp.cname='英语' then grade else 0 end) 英语,
           max(case when temp.cname='物理' then grade else 0 end) 物理

     from
    (select  sname,cname,grade from tb_stu s, tb_couse c,tb_sc sc where s.sno=sc.s_no and c.cno=sc.c_no) temp
    group by temp.sname
    order by temp.sname;

  相关解决方案