mysql行转列 博客分类: 数据库 MySQL
select name,
sum(case subject when'chinese'then score else0end)as'chinese',
sum(case subject when'math'then score else0end)as'math',
sum(case subject when'english'then score else0end)as'english'
from user
groupby name;
create procedure line_to_col()
begin
declare i int;
declare _chinese int;
declare _math int;
declare _english int;
declare _name varchar(10);
declare test_cursor CURSOR forselect name from user;
select count(*)into i from user;
CREATE TEMPORARY TABLE tmp_tab(
name varchar(10),
chinese_score int,
math_socre int,
english_score int);
if i>0then
open test_cursor;
repeat fetch test_cursor into _name;
select score into _chinese from user where subject ='chinese'and name =_name;
select score into _math from user where subject ='math'and name =_name;
select score into _english from user where subject ='english'and name =_name;
insert into tmp_tab values(_name,_chinese,_math,_english);
set i=i-1;
until i=0end repeat;
close test_cursor;
endif;
select DISTINCT *from tmp_tab;
drop table tmp_tab;
end
推荐阅读
-
mysql行转列 博客分类: 数据库 MySQL
-
mysql行转列 博客分类: 数据库 MySQL
-
【mysql】高可用mysql笔记一 博客分类: mysql mysql
-
解决远程连接mysql错误1130 博客分类: mysql mysql
-
解决远程连接mysql错误1130 博客分类: mysql mysql
-
【mysql】mac修改mysql5.7配置&开启binlog 博客分类: mysql mysql
-
left join on 和 where 的使用 博客分类: sqlsql mysql oracle lefton
-
MySQL LEFT JOIN 语法 博客分类: MySQL leftjoinmysql
-
mysql的left,right,substr,instr截取字符串,截取小数点float 博客分类: mysql leftrightsubstrinstrfloat
-
Grails连MySQL - 连接池超时问题 博客分类: GrailsMySQL grailsmysql