本文目录一览:
MySQL 多表查询
我不是很了解你想要的是怎么样。以下是我的思路: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
(注意:要修改*号列出你想列的列名,并改一下选项里面的列名)
“mysql ”多表联合查询语句怎么写?
一使用SELECT子句进行多表查询SELECT 字段名 FROM 表1,表2 … WHERE 表1字段 = 表2字段 AND 其它查询条件SELECT a.id,a.name,a.address,a.date,b.math,b.english,b.chinese FROM tb_demo065_tel AS b,tb_demo065 AS a WHERE a.id=b.id
注:在上面的的代码中,以两张表的id字段信息相同作为条件建立两表关联,但在实际开发中不应该这样使用,最好用主外键约束来实现。
1、联合查询可合并多个相似的选择查询的结果集。等同于将一个表追加到另一个表,从而实现将两个表的查询组合到一起,使用谓词为UNION或UNION ALL。联合查询时,查询结果的列标题为第一个查询语句的列标题。因此,要定义列标题必须在第一个查询语句中定义。要对联合查询结果排序时,也必须使用第一查询语句中的列名、列标题或者列序号。
2、在使用UNION 运算符时,应保证每个联合查询语句的选择列表中有相同数量的表达式,并且每个查询选择表达式应具有相同的数据类型,或是可以自动将它们转换为相同的数据类型。在自动转换时,对于数值类型,系统将低精度的数据类型转换为高精度的数据类型。
3、在包括多个查询的UNION语句中,其执行顺序是自左至右,使用括号可以改变这一执行顺序。例如:查询1 UNION (查询2 UNION 查询3)。
mysql数据库,多个表的查询操作
要点:left
join,right
join,inner
join
首先有如下两个表:
student:
id(int)
name(nvarchar)
1
a
2
b
3
c
4
d
5
e
6
f
quiz:
id(int)
score(int)
1
60
2
70
4
80
6
90
8
100
9
30
内连接:(inner
join)包括连接表的匹配行
select
student.name,quiz.score
from
quiz
inner
join
student
on
student.id=quiz.id
name
score
a
60
b
70
d
80
f
90
左连接:(left
join)包括连接表匹配行以及左连接表的所有行
select
student.name,quiz.score
from
student
left
join
quiz
on
student.id=quiz.id
name
score
a
60
b
70
c
null
d
80
e
null
f
90
右连接:(right
join)结果包括连接表的匹配行以及右连接表的所有行
select
student.name,quiz.score
from
student
right
join
quiz
on
student.id=quiz.id
name
score
a
60
b
70
d
80
f
90
null
100
null
30
当然,也可以看出左连接也可以写成右连接的形式:
select
student.name,quiz.score
from
student
right
join
quiz
on
student.id=quiz.id等价于
select
student.name,quiz.score
from
quiz
left
join
student
on
student.id=quiz.id