Mysql 如何连接具有相同表但条件不同的另一个表
我想展示一所学生毕业的学校。我有一张学校名称表和一张学生档案表。这是我的密码: 学校\u dbMysql 如何连接具有相同表但条件不同的另一个表,mysql,sql,inner-join,Mysql,Sql,Inner Join,我想展示一所学生毕业的学校。我有一张学校名称表和一张学生档案表。这是我的密码: 学校\u db shc_id shc_title 1 School A 2 School B 3 School C 4 School D 5 School E stu_id stu_school1 stu_school2 stu_school3 1
shc_id shc_title
1 School A
2 School B
3 School C
4 School D
5 School E
stu_id stu_school1 stu_school2 stu_school3
1 1 2 2
2 1 2 4
3 2 2 4
student\u db
shc_id shc_title
1 School A
2 School B
3 School C
4 School D
5 School E
stu_id stu_school1 stu_school2 stu_school3
1 1 2 2
2 1 2 4
3 2 2 4
所以我写:
select school_db.sch_title as school from school_db
inner join student_db on student_db.stu_school1=school_db.shc_id
inner join student_db on student_db.stu_school2=school_db.shc_id
inner join student_db on student_db.stu_school3=school_db.shc_id
where student_db.stu_id='1'
但是我没有得到正确的结果。因此,请您建议如何在这种情况下使用正确的连接
我预计结果如下:
stu_id stu_school1 stu_school2 stu_school3
1 School A School B School B
2 School A School B School D
3 School B School B School D
关于,每个重新联接必须具有唯一的别名:
INNER JOIN student_db AS db1 ON school_db.shc_id = db1.stu_school1
^^^^^^ ^^^
INNER JOIN student_db AS db2 etc...
至于结果,您需要的是pivot查询,MySQL不直接支持。有一些变通办法,但它们很难看,很难维护。最好先执行常规查询,然后在客户端中执行表格式设置。每个重新联接必须具有唯一的别名:
INNER JOIN student_db AS db1 ON school_db.shc_id = db1.stu_school1
^^^^^^ ^^^
INNER JOIN student_db AS db2 etc...
至于结果,您需要的是pivot查询,MySQL不直接支持。有一些变通办法,但它们很难看,很难维护。您最好先执行常规查询,然后在客户端中执行表格式设置。您应该在表
student\u db
上三次加入表school\u db
,这样您就可以获得表student\u db
上每列的值
还有一件事,您应该在表school\u db
上定义唯一的别名,以便服务器可以识别表和列已加入的对象
SELECT a.stu_id,
b.shc_title sc1,
c.shc_title sc2,
d.shc_title sc3
FROM student_db a
INNER JOIN school_db b
ON a.stu_school1 = b.shc_id
INNER JOIN school_db c
ON a.stu_school2 = c.shc_id
INNER JOIN school_db d
ON a.stu_school3 = d.shc_id
WHERE a.stu_id = '1'
school\u db
上三次加入表student\u db
,以便您可以获得表student\u db
上每列的值
还有一件事,您应该在表school\u db
上定义唯一的别名,以便服务器可以识别表和列已加入的对象
SELECT a.stu_id,
b.shc_title sc1,
c.shc_title sc2,
d.shc_title sc3
FROM student_db a
INNER JOIN school_db b
ON a.stu_school1 = b.shc_id
INNER JOIN school_db c
ON a.stu_school2 = c.shc_id
INNER JOIN school_db d
ON a.stu_school3 = d.shc_id
WHERE a.stu_id = '1'
SELECT
student_db.stu_id,
s1.sch_title as school1,
s2.sch_title as school2,
s3.sch_title as school3
FROM
student_db
INNER JOIN school_db s1 ON student_db.stu_school1=s1.shc_id
INNER JOIN school_db s2 ON student_db.stu_school2=s2.shc_id
INNER JOIN school_db s3 ON student_db.stu_school3=s3.shc_id
WHERE student_db.stu_id='1'
但如果不总是有3所学校,你应该展示:
SELECT
student_db.stu_id,
s1.sch_title as school1,
IFNULL(s2.sch_title, 'No school selected') as school2,
IFNULL(s3.sch_title, 'No school selected') as school3
FROM
student_db
INNER JOIN school_db s1 ON student_db.stu_school1=s1.shc_id
LEFT JOIN school_db s2 ON student_db.stu_school2=s2.shc_id
LEFT JOIN school_db s3 ON student_db.stu_school3=s3.shc_id
WHERE student_db.stu_id='1'
您的错误是将一个学生与一个学校表连接3次,而您的问题是一个学生有3个学校:
SELECT
student_db.stu_id,
s1.sch_title as school1,
s2.sch_title as school2,
s3.sch_title as school3
FROM
student_db
INNER JOIN school_db s1 ON student_db.stu_school1=s1.shc_id
INNER JOIN school_db s2 ON student_db.stu_school2=s2.shc_id
INNER JOIN school_db s3 ON student_db.stu_school3=s3.shc_id
WHERE student_db.stu_id='1'
但如果不总是有3所学校,你应该展示:
SELECT
student_db.stu_id,
s1.sch_title as school1,
IFNULL(s2.sch_title, 'No school selected') as school2,
IFNULL(s3.sch_title, 'No school selected') as school3
FROM
student_db
INNER JOIN school_db s1 ON student_db.stu_school1=s1.shc_id
LEFT JOIN school_db s2 ON student_db.stu_school2=s2.shc_id
LEFT JOIN school_db s3 ON student_db.stu_school3=s3.shc_id
WHERE student_db.stu_id='1'
@还有一件事,如果表
student\u db
上的schoolID列的数量增加,最好使用LEFT JOIN
而不是internal JOIN
,这样即使学生在school\u db
上没有匹配的学校,也会显示在结果列表上,如果表student\u db
上的schoolID列的值为0,最好使用LEFT JOIN
而不是internal JOIN
,这样即使学生在school\u db
上没有匹配的学校,也会显示在结果列表上。