连接3个表,包括空值Mysql

时间:2019-07-17 12:27:44

标签: mysql sql

我们在MySQL中有三个表 我们查询的对象必须是

1) student 
2) subject
3) mark

学生

id name class year  gender address
1 Ganesh Wagh SSC 2010 male Bhandup, Maharashtra
2 Shailesh Penkar HSC 2010 male Ghatkoper, Mumbai
3 Pooja Palshetkar BE 2014 female Vichumbe, Panvel

主题

id name
1 English
2 Hindi
3 Maths

标记

id studentid subjectid mark
1 1 1 70
2 1 2 80
3 1 3 90
4 2 1 60
5 2 2 50
6 2 3 60

我想要的输出

Name        subject mark
Ganesh Wagh English 70
Ganesh Wagh Hindi 80
Ganesh Wagh Maths 90
Shailesh Penkar English 60
Shailesh Penkar Hindi 50
Shailesh Penkar Maths 60
Pooja Palshetkar English 0
Pooja Palshetkar Hindi 0
Pooja Palshetkar Maths 0

我尝试了这个但没有解决

SELECT 
  student.name, 
  subject.name, 
  mark.mark 
FROM 
(mark AS mark 
RIGHT JOIN student AS student 
  ON student.id = mark.studentid) 
LEFT JOIN subject AS subject 
  ON mark.subjectid = subject.id 
ORDER BY student.id ASC

请帮助, 谢谢进阶

2 个答案:

答案 0 :(得分:2)

您可能想进行交叉联接,以便获得所有科目和所有学生,无论标记中是否有值。像

select ...
from (student st cross join subject su)
      left join mark m on st.id = mark.studentid and su.id = mark.subjectid
order by student id asc

答案 1 :(得分:0)

您也可以按照自己的方式进行操作,如果这是您的要求,则只需取出“ AS”即可。如果需要别名,则只需在表名后面写入它。在这种情况下,名称是相同的,所以就省去吧。

var startPoint = [51.263969,-0.527153]
var endPoint = [51.258813,-0.505876]
var vector = [startPoint,endPoint]
var metres = 100