Wednesday 5 September 2018

Mysql INNER JOIN does not work for me

I'm trying to make my SQL query work and I fail, so I decided to ask more experienced and familiar with SQL people since I'm not so.

What I have: 2 tables in my DB, one is "DEV" table that contains: id, lat, lon, login, password second one is "TASK" table which contains: id, lat, lon, address, id_dev. Id_dev is a foreign key to table "DEV".
What I'm trying to do is: Make query to get all DEVs that have NO task assigned (there is no record in table "task" with given dev.id) and get another list of DEVs that have tasks.
I want them separated. I tried something from a tutorial:
SELECT * FROM `dev` INNER JOIN 'task' ON dev.id=task.id_dev ORDER BY dev.id;

But it didn't work for me. Any suggestions please? Kind regards!

If you want the 'dev' records with no 'task' you shouldn't use INNER JOIN as that brings back the intersection of the sets.
One option is to use a LEFT JOIN, so something like:
SELECT      dev.* 

FROM        dev

LEFT JOIN   task
    ON      dev.id=task.id_dev 

WHERE       task.id_dev IS NULL

ORDER BY    dev.id;

0 comments:

Post a Comment