简体   繁体   English

如何联接两个表并从第一个表返回不在第二个表中的值?

[英]How do I join two tables and return the values from the first that aren't in the second table?

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. 我有两个表,第一个表包含id和employee_name,第二个表包含employee_id(与id相同)和其他一些列。 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? 如何将第一个表与第二个表连接在一起,以便可以输出第一个表中没有出现的所有雇员ID(以及其余列)? 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. 您的表在SQL中被翻转。 Based on your narrative, the SQL should be: 根据您的叙述,SQL应该是:

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 您无需使用连接,只需使用“ not in”子句

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 . 如果我正确理解了您的问题,则想从data1获取记录,而data2的列id中不存在employee_id的值。

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. 当前查询的结构方式要求您输入employee_id ... where employee_id is NULL这样只会返回空记录。 Instead, make a query of data1 like this: 相反,对data1如下查询:

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. 这将使用子查询在data2查找id值列表,然后仅从data1返回其employee_id不存在的记录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何连接两个表并仅从第二个表中获取值(如果存在)? - How do I join two tables and only take values from the second table if they exist? 如何在SQL中联接两个表,以根据第一个表限制第二个表数据? - How do I join two tables in SQL limiting the second table data depending on the first one? 如何内部联接2个SQL表,但仅从第二个表中获取第一个结果? - How do I inner join 2 SQL tables, but only take the first result from the second table? 连接两个表并将第二个表中的值分配给第一个表的每个日期 - Join two tables and assign values from second table to each date of the first table 在多个列上连接两个表并仅返回第一个表中不在第二个表中的记录 - Join two tables on multiple columns and return only records from first table that are not in the second 连接两个表,如果第一个表的值为空,但第二个表具有值,则取第二个表的值 - Join two tables and if the value on the first table is null, but second table has values, take the values of the second table 联接两个表,将值插入第二个表 - Join two tables, Inserting values into second table 如何查询两个表并从第一个表返回所有记录,而不管第二个表中是否存在 - How to query two tables and return all records from first, regardless on whether exist in the second table SQL查询连接两个表,其中第一个表行具有1-Many关系,第二个表值跟随行 - SQL Query Join two tables with 1-Many relation with first table row and following rows for second table values 你能加入两个表并得到一个包含 obj 列表的 obj(来自第一个表)(来自第二个表) - can you join two tables and result with an obj (from first table) containing a list of obj(from the second table)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM