1

I have two tables tracklist and song. I am fetching all values from song having sid as primary and tracklist having sid as foreign. Though fetching all values from song using this query is successfull.

SELECT t.sid as tsid, s.sid as ssid, s.name from song s LEFT JOIN tracklist t ON s.sid=t.sid;

But If I wish to fetch those values from song that are not in tracklist this query is not fetching single row.

SELECT t.sid as tsid, s.sid as ssid, s.name from song s LEFT JOIN tracklist t ON s.sid=t.sid where t.sid= NULL;
4
  • Have you tried using a JOIN or a INNER JOIN instead of a LEFT JOIN? Commented Aug 9, 2011 at 18:34
  • I don't know why your second query is failing (it looks ok to me) but I'd use an inner join for the first query. Commented Aug 9, 2011 at 18:37
  • 1
    @JMichel no it's legitimate to use a left join to find rows not in a table. The mysql doc even mentions it: "If you use LEFT JOIN to find rows that do not exist in some table ..." Commented Aug 9, 2011 at 18:38
  • For the second query, try using tsid instead of t.sid in the WHERE clause Commented Aug 9, 2011 at 18:50

4 Answers 4

3

Rather than =NULL, use IS NULL:

SELECT t.sid as tsid, s.sid as ssid, s.name from song s LEFT JOIN tracklist t ON s.sid=t.sid where t.sid IS NULL;
Sign up to request clarification or add additional context in comments.

Comments

2

I think you want to use IS NULL not = NULL

e.g:

SELECT t.sid as tsid, s.sid as ssid, s.name 
FROM song s 
LEFT JOIN tracklist t ON s.sid=t.sid 
WHERE t.sid IS NULL

Comments

0

I think this should work:

SELECT t.sid as tsid, s.sid as ssid, s.name 
from song s LEFT OUTER JOIN tracklist t ON s.sid=t.sid where t.sid IS NULL; 

2 Comments

LEFT OUTER JOIN is just another way of using LEFT JOIN. Some language use one, some the other, not both.
I use (+) myself in oracle, LEFT JOIN/INNER JOIN ... terminology always confuses me. Thanks for the correction. The idea is to select everything from song but only tracklist which are null
0
SELECT t.sid as tsid, s.sid as ssid, s.name from song s LEFT JOIN tracklist t ON s.sid<>t.sid;

its checking if the id of s.sid is null. if you didn't give the id then its not going to return it.

<> means not equal...

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.