1

I am new Sql Queries.I wanted to make a table from difference of two tables.
Here's my query:

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

Please help.

Harpreet Singh
  • 430
  • 1
  • 8
  • 24
  • If you need the where clause that should be always after the join clause and where makes a left join to an inner join. You need to provide some sample data and expected result out of it. – Abhik Chakraborty Sep 14 '15 at 11:54

2 Answers2

2

Move the first WHERE clause to the end, and move that other where condition to ON to get true LEFT JOIN:

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

Alternative syntax:

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
Harpreet Singh
  • 430
  • 1
  • 8
  • 24
jarlh
  • 40,041
  • 8
  • 39
  • 58
1

This post will help you to understand better the joins. It help me too.

Explanation about Joins click here.

enter image description here

Community
  • 1
  • 1