user1660666
user1660666

Reputation: 5

find non-paired values on table join Mysql

I have two tables that I am joining on the emp. However I am also trying to find when the join pairing doesn't happen.

table: emp
+--------------------+
| emp                |
+--------------------+
| 121211             |
| 121212             |
| 121213             |
| 121214             |
+--------------------+

table: empskills
+--------------------+------------------+
| emp            | skill            |
+--------------------+------------------+
| 121211             | 15               |
| 121212             | 15               |
+--------------------+------------------+

SELECT e.emp, es.skill FROM emp e
    JOIN empskills es
    ON es.emp = e.emp;

So I am trying to locate the employees that are in the table with the skill they possess along with those that aren't in the table. Those that aren't in the table will just be given a "null" value to the es.skill column.

Upvotes: 0

Views: 42

Answers (1)

juergen d
juergen d

Reputation: 204766

Use a LEFT JOIN

SELECT e.emp, es.skill 
FROM emp e
LEFT JOIN empskills es ON es.emp = e.emp
WHERE es.skills > 0 
   OR es.skills IS NULL

See this great explanation of joins

Upvotes: 2

Related Questions