2015-09-14 144 views
1

我是新的Sql查詢。我想從兩個表的差異中創建一個表。
這裏是我的查詢:MySql查詢:左加入

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

請幫助。

+0

如果您需要在where子句中加入條款之後,應該是始終在哪裏,使左連接到內連接。您需要提供一些樣本數據和預期結果。 –

回答

2

移動第一WHERE子句結束,並移動其他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