MySQL 多表查询

MySQL 多表查询,第1张

我不是很了解你想要的是怎么样。以下是我的思路: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 查出单选

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

你这几张表的数据会频繁变动吗?

如果不会频发变动,直接做成视图,从视图查询是最简单高效的

如果数据会频繁的发生变动,视图就不太适合了,可以选择连表查询,注意关联字段做好索引(如果已经存在外键关联,不需要再建索引)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/8672415.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-19
下一篇 2023-04-19

发表评论

登录后才能评论

评论列表(0条)

保存