1、在mysql环境,创建数据库表。
2、插入测试数据。
3、编写目标sql。mysql中,查询纵向转横向可用casewhen语句。
SELECT fields INTO OUTFILE 'file_name'[{FIELDS | COLUMNS} 字段
[TERMINATED BY 'string'] 字段之间分隔符号
[[OPTIONALLY] ENCLOSED BY 'char'] 字段被包含在char中间
[ESCAPED BY 'char'] 忽略字段里出现的char
]
[LINES
[STARTING BY 'string'] 忽略开头是string的行
[TERMINATED BY 'string'] 行分隔符
]
FROM test_table
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
[REPLACE | IGNORE] 遇到重复的时候处理方法,替换或者是忽略
INTO TABLE tbl_name 导入数据的目的表名
[PARTITION (partition_name,...)] 分区选择
[CHARACTER SET charset_name] 字符集
[{FIELDS | COLUMNS} 字段
[TERMINATED BY 'string'] 字段之间分隔符号
[[OPTIONALLY] ENCLOSED BY 'char'] 字段被包含在char中间
[ESCAPED BY 'char'] 忽略字段里出现的char
]
[LINES
[STARTING BY 'string'] 忽略开头是string的行
[TERMINATED BY 'string'] 行分隔符
]
[IGNORE number {LINES | ROWS}] 忽略行/列
[(col_name_or_user_var,...)] 目的表的表字段名或者用户变量名
[SET col_name = expr,...] 设置表字段值
1、在mysql环境,创建数据库表,
create table test_data1(id int, name VARCHAR(20), day VARCHAR(20))
2、插入测试数据,
insert into test_data1 values(1,'liu',1)
insert into test_data1 values(2,'liu',3)
insert into test_data1 values(3,'wang',1)
insert into test_data1 values(4,'wang',2)
insert into test_data1 values(4,'wang',4)
3、查询表中数据,select * from test_data1;
4、编写目标sql;
select name,
max(case when day=1 then 1 end) d_1,
max(case when day=2 then 2 end) d_2,
max(case when day=3 then 3 end) d_3,
max(case when day=4 then 4 end) d_4
from test_data1 t group by name
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)