Harpreet Singh
Harpreet Singh

Reputation: 562

MySql Query: Left Join

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.

Upvotes: 1

Views: 1441

Answers (2)

jarlh
jarlh

Reputation: 44706

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

Upvotes: 2

user3812703
user3812703

Reputation:

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

Explanation about Joins click here.

enter image description here

Upvotes: 1

Related Questions