先谢谢你。
我有这个右连接工作得很好,但当我试图把它改成左连接,我得到了一个错误。
问题是从一个名为sc的学生成绩表中,从1班比2班成绩高的学生表中获得关于学生的所有信息。
学生(sid,sname,sage,ssex)
sc(sid、cid、得分)
这是我的右连接:
SELECT * FROM Student RIGHT JOIN (
SELECT t1.SId, class1, class2 FROM
(SELECT SId, score as class1 FROM sc WHERE sc.CId = '01')AS t1,
(SELECT SId, score AS class2 FROM sc WHERE sc.CId = '02')AS t2
WHERE t1.SId = t2.SId
AND t1.class1 > t2.class2
)r
ON Student.SId = r.SId;
然后我尝试了以下方法:
SELECT t1.SId, class1, class2, Student.* FROM
(SELECT SId, score as class1 FROM sc WHERE sc.CId = '01')AS t1,
(SELECT SId, score AS class2 FROM sc WHERE sc.CId = '02')AS t2
WHERE t1.SId = t2.SId
AND t1.class1 > t2.class2
)r
LEFT JOIN Student
ON Student.SId = r.SId;
但没用。
我对sql很陌生,希望你能解释一下。
我正在使用mysql5.7
2条答案
按热度按时间8zzbczxx1#
通常,如果您有一个正确的连接查询:
您只需将其更改为left join query,如下所示:
对于您的情况,当您将查询更改为left join时,您更改了select\语句,它会出错。
只需将上述内容应用于您的查询:
tgabmvqs2#
第一个有效的查询是:
table
r
正在通过此子查询创建:SELECT t1.SId, class1, class2, Student.* FROM
(SELECT SId, score as class1 FROM sc WHERE sc.CId = '01')AS t1,
(SELECT SId, score AS class2 FROM sc WHERE sc.CId = '02')AS t2
WHERE t1.SId = t2.SId
AND t1.class1 > t2.class2
)r
LEFT JOIN Student
ON Student.SId = r.SId;
SELECT * from
(SELECT t1.SId, class1, class2 FROM
(SELECT SId, score as class1 FROM sc WHERE sc.CId = '01')AS t1,
(SELECT SId, score AS class2 FROM sc WHERE sc.CId = '02')AS t2
WHERE t1.SId = t2.SId
AND t1.class1 > t2.class2
)r
LEFT JOIN Student
ON Student.SId = r.SId;