select A1.survey_id, A1.topic, A1.qid, B1.options, B1.description from survey_multiple_choices as A1 left join survey_multiple_choices_option as B1 on A1.id = B1.single_choice_id 查出多选
select S.name, S.description, S.status, C1.topic, C1.qid, C1.options, C1.description from survey as S left join (select A.survey_id, A.topic, A.qid, B.options, B.description from survey_single_choice as A left join survey_single_choice_option as B on A.id = B.single_choice_id) as C1 on S.id = C1.survey_id 单选合并到问卷
select S2.name, S2.description, S2.status, C2.topic, C2.qid, C2.options, C2.description from survey as S2 left join (select A1.survey_id, A1.topic, A1.qid, B1.options, B1.description from survey_multiple_choices as A1 left join survey_multiple_choices_option as B1 on A1.id = B1.single_choice_id) as C2 on S2.id = C2.survey_id 多选合并到问卷
select S3.name, S3.description, S3.status, B2.topic, B2.qid from survey as S3 left join survey_short_answer as B2 on S3.id = B2.survey_id 简答表合并到问卷
如果你是要多行列出 问卷名 题号 题目select S.name, S.description, S.status, C1.topic, C1.qid from survey as S left join (select A.survey_id, A.topic, A.qid, B.options, B.description from survey_single_choice as A left join survey_single_choice_option as B on A.id = B.single_choice_id) as C1 on S.id = C1.survey_idUNION ALLselect S2.name, S2.description, S2.status, C2.topic, C2.qid from survey as S2 left join (select A1.survey_id, A1.topic, A1.qid, B1.options, B1.description from survey_multiple_choices as A1 left join survey_multiple_choices_option as B1 on A1.id = B1.multiple_choices) as C2 on S2.id = C2.survey_idUNION ALLselect S3.name, S3.description, S3.status, B2.topic, B2.qid from survey as S3 left join survey_short_answer as B2 on S3.id = B2.survey_idORDER BY name, qid ASC
如果是一条列出select * from (select * from (select S.name, S.description, S.status, C1.topic, C1.qid, C1.options, C1.description from survey as S left join (select A.survey_id, A.topic, A.qid, B.options, B.description from survey_single_choice as A left join survey_single_choice_option as B on A.id = B.single_choice_id) as C1 on S.id = C1.survey_id) as D left join (select S2.name, S2.description, S2.status, C2.topic, C2.qid, C2.options, C2.description from survey as S2 left join (select A1.survey_id, A1.topic, A1.qid, B1.options, B1.description from survey_multiple_choices as A1 left join survey_multiple_choices_option as B1 on A1.id = B1.single_choice_id) as C2 on S2.id = C2.survey_id) as D1 on D.name = D1.name) as E left join (select S3.name, S3.description, S3.status, B2.topic, B2.qid from survey as S3 left join survey_short_answer as B2 on S3.id = B2.survey_id) E1 on E.name = E1.name
(注意:要修改*号列出你想列的列名,并改一下选项里面的列名)
你的图片看不怎么清楚,我给你写个栗子select b.classname,count(a.sno) as 总人数 from student a,classes b where a.sno=b.sno group by b.classname
创建表,数据
create table a(id int,
项目号 varchar(2),
总金额 int,
上家单位 varchar(4))
insert into a values (1,'01',100,'AAAA')
insert into a values (2,'02',200,'BBBB')
insert into a values (3,'03',250,'CCCC')
create table b
(id int,
项目号 varchar(2),
到款 int,
到款时间 date)
insert into b values (1,'03',50,'2018-07-23')
insert into b values (2,'03',100,'2018-07-24')
insert into b values (3,'01',80,'2018-07-25')
执行:
SELECTa.项目号,
a.总金额,
sum(
CASE
WHEN a.项目号 = b.项目号 THEN
到款
ELSE
0
END
) 到款总额
FROM
a
LEFT JOIN b ON a.项目号 = b.项目号
GROUP BY
a.项目号,
a.总金额
结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)