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.

1
  • 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. Commented Sep 14, 2015 at 11:54

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Explanation about Joins click here.

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.