Urgent LEFT JOIN Help needed

Hello,
i have attached 2 tables.
i want to pull all the records out of table1 that does not match with table 2 by comparing dep_id and sec_id.

this does not work for me :frowning:

$query_copy="SELECT *
           FROM table1
           LEFT JOIN table 2
           ON table1.dep_id=table2.dep_id
           AND table1.sec_id=table2.sec_id

what is wrong?

Your query takes all rows from table 1, even if there is no corresponding row in table 2.

sorry i am not getting a right way :frowning:

That was a hint :wink:

Switch table 1 and table 2 in your query. Then you’ll get all rows from table 2, even if there is no corresponding row in table 1.

Then you’ll get all rows from table 2

actually i only want records from table one. not from table 2 :slight_smile:

Ah yes, I got confused by all the 1’s and 2’s… :lol:

To get only the rows with no corresponding row in table 2, check if any of the table 2 joined columns is NULL.

Somthing like this :slight_smile:

$query_copy="SELECT *
           FROM table1
           LEFT JOIN table2
           ON table1.dep_id=table2.dep_id
		   AND table1.sec_id=table2.sec_id
		   WHERE table2.dep_id=NULL
		   AND table2.sec_id=NULL
           ";

seems not working…

One where condition is enough, you choose which one :wink:

And change = NULL into IS NULL