Tuesday 4 September 2018

Mysql: Select with Left Join does not work as expected

I have a query of the following type:

select * from tbl_1 where [cond] as aliasA LEFT JOIN tbl_2 as aliasB
 ON (aliasA.id = aliasB.id) WHERE aliasB.id IS NULL

It appears that it's working except that it's ignoring the last WHERE aliasB.id IS NULL. So, it's just returning the results of:
select * from tbl_1 where cond as aliasA LEFT JOIN tbl_2 as aliasB
 ON (aliasA.id = aliasB.id)

How would I change the above query to get the results of querying tbl_1 where [cond] displaying only the rows that are not in tbl_2?
Thanks in advance!

You could try:
SELECT * FROM tbl_1 aliasA LEFT JOIN tbl_2 aliasB
ON aliasA.id = aliasB.id
WHERE condA
  AND aliasB.id IS NULL

0 comments:

Post a Comment