I have two tables, the first containing the id and employee_name, and the second containing the employee_id (same as id) and some other columns. How do I join the first table with the second so that I can output all the employee ID's (and the rest of the columns) in the first table that don't appear in the second? The query I've gotten to is this:
select employee_id
from data2 left join data1 on data2.employee_id = data1.id
where employee_id is NULL
This outputs the correct number of rows, but there are no values in them. Any help is greatly appreciated!
Your tables are flipped in your SQL. Based on your narrative, the SQL should be:
select data1.*
from data1
left join data2 on data2.employee_id = data1.id
where data2.employee_id is NULL
You need not use join, you can simply use 'not in' clause
select id
from table1
where id not in (select employee_id from table2)
Let me know, if it works.
If I understand your question correctly, you want to get the records from data1
whose values of employee_id
do not exist in the column id
in data2
.
The way the current query is structured, you are asking for employee_id ... where employee_id is NULL
so that will only return null records. Instead, make a query of data1
like this:
select * from data1 where employee_id not in (select id from data2)
This uses the subquery to find a list of id
values in data2
and then only returns records from data1
whose employee_id
does not exist in that list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.