case when then end 用法
多行转一行多列
select name,max(
case course
when 'java' then score
end) Java, max(
case course
when 'MySQL' then score
end) MySQL
from test_9
group by name;
多行转一行一列
相关函数
- concat(值,’拼接符’,值 ) : 拼接,多行数据只会拼接一行
- group_concat(值,’拼接符’,值 ) : 拼接,多行压扁到一行
select name,
group_concat(course,'=',score order by course asc separator ' | ') as '各科成绩'
from test_9 group by name;
多表查询
inner join
select *
from teacher tea
inner join student stu on tea.id = stu.teacher_id;
使用内连接的话,会以左边表为基准(student),生成新视图的时候,先生成左边表中的数据,然后再去匹配右边表中是否有符合条件的,没有的话,就不生成这一行
同时左表中有的,右表中没有的数据,都不会生成,右表中有的,左表中没有的也一样不会生成
left join
select * from student s
left join teacher t on s.teacher_id = t.id;
以左边的表为基准,左表中数据都有,右表中不符合条件的就没有,就在指定列上用null代替
right join
select * from student s
right join teacher t on s.teacher_id = t.id;
以右表为基准,右表中数据都有,左表中不符合条件的就没有,就在指定列上用null代替