3

How I can join two tables when record in joined table doesn't exist. I'm tried something like this:

SELECT 
 Confessions.ID, Confessions.WriteTitle, Confessions.WriteArea,
 Confessions.POST_DATE, Votes.Type
FROM Confessions LEFT OUTER JOIN Votes ON Confessions.ID = Votes.PostID 
WHERE ForUser = :ForUser AND Status = :Status AND Votes.IP_Check = :IP_Check

but when record from votes with PostID like ID in Confessions doesn't exist MySQL gives back empty result for this record. isthere any other way?

RolandoMySQLDBA
185k34 gold badges327 silver badges541 bronze badges
asked Jun 22, 2015 at 19:44

1 Answer 1

6

A WHERE-condition on the inner table will change the the result to an Inner Join instead. You need to move this condition to the ON:

SELECT 
 Confessions.ID, Confessions.WriteTitle, Confessions.WriteArea,
 Confessions.POST_DATE, Votes.Type
FROM Confessions LEFT OUTER JOIN Votes 
 ON Confessions.ID = Votes.PostID 
 AND Votes.IP_Check = :IP_Check
WHERE ForUser = :ForUser AND Status = :Status 

As a rule of thumb: Normally conditions on the Outer table are in WHERE while condition on the Inner table are in ON.

answered Jun 22, 2015 at 19:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.