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
这是最典型的自关联,应该是这样的:SELECT a.user_id, a.content, b.content, c.content, d.content,
e.content, f.content, g.content, h.content, i.content
FROM sj_reg_extend_info a,
sj_reg_extend_info b,
sj_reg_extend_info c,
sj_reg_extend_info d,
sj_reg_extend_info e,
sj_reg_extend_info f,
sj_reg_extend_info g,
sj_reg_extend_info h,
sj_reg_extend_info i
WHERE a.reg_field_id=1
AND b.reg_field_id=6 AND b.user_id=a.user_id
AND c.reg_field_id=9 AND c.user_id=a.user_id
AND d.reg_field_id=10 AND d.user_id=a.user_id
AND e.reg_field_id=11 AND e.user_id=a.user_id
AND f.reg_field_id=4 AND f.user_id=a.user_id
AND g.reg_field_id=5 AND g.user_id=a.user_id
AND h.reg_field_id=7 AND h.user_id=a.user_id
AND i.reg_field_id=8 AND i.user_id=a.user_id
我仅提供思路给你,至于其它单位,可以单独摘录出来,union allselect 始发地 目的地 起步价,sum(case when 区间 >0 and 区间 <= 20 then 单位价格 else '' end) '0-20(公斤)' ,
sum(case when 区间 >20 and 区间 <= 50 then 单位价格 else '' end) '20-50(公斤)' ,
sum(case when 区间 >50 and 区间 <= 100 then 单位价格 else '' end) '50-100(公斤)' from (你的sql语句) t group by t.始发地 ,t.目的地 ,t.起步价
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)