MySql查询:左连接

时间:2015-09-14 11:52:57

标签: mysql sql

我是新的Sql Queries。我想根据两个表的不同来制作一个表 这是我的疑问:

SELECT * FROM `question` WHERE `relatedFields` = 'Math' LEFT JOIN  `answer` ON `question`.ques = `answer`.ques where `answer`.TeacherNumber=1111111111

请帮忙。

2 个答案:

答案 0 :(得分:2)

将第一个WHERE子句移到最后,并将其他条件移到ON以使其成为真LEFT JOIN

SELECT *
FROM `question`
  LEFT JOIN  `answer` ON `question`.ques = `answer`.ques
     and `answer`.TeacherNumber=1111111111
where `question`.relatedFields = 'Math'

替代语法:

SELECT *
FROM
    (select * from `question` WHERE `relatedFields` = 'Math') as q
LEFT JOIN
    (select * from `answer` where TeacherNumber = 1111111111) as a
  ON q.ques = a.ques

答案 1 :(得分:1)

这篇文章将帮助您更好地理解联接。它也对我很有帮助。

Explanation about Joins click here.

enter image description here