简体   繁体   English

两个共有2列的SQL表需要在表A中但不在表B中的记录(列1相同,但列2不同)

[英]two SQL tables with 2 columns in common, need records that are in table A but not in table B (column 1 is same in both but different column 2)

I have two tables, tableA and tableB. 我有两个表,tableA和tableB。 They both have columns (vehicle_make and vehicle_model). 它们都有列(vehicle_make和vehicle_model)。

I need all make/model of the vehicles from tableA that are not present in tableB. 我需要tableA中所有不在tableB中的车辆的品牌/型号。

Basically I need to find new make and models. 基本上我需要找到新的品牌和型号。 tableB is currently I am using in my project and tableA is generic data with all the vehicles in the US. 我目前在我的项目中使用的是tableB,而tableA是美国所有车辆的通用数据。

You can use NOT IN operator. 您可以使用NOT IN运算符。

SELECT DISTINCT vehicle_make, vehicle_model
FROM tableA
WHERE (vehicle_make || vehicle_model) NOT IN 
      (SELECT DISTINCT (vehicle_make || vehicle_model)
       FROM tableB)

Using a coorlated subquery: usually the most efficient with exists. 使用协调的子查询:通常最高效的with存在。

SELECT vehicle_make, vehicle_model
FROM tableA A
WHERE Not Exists (SELECT * 
                  FROM tableB 
                  WHERE A.vehicle_make = B.Vehicle_make
                    and A.vehicle_model = B.Vehicle_model

Using an outer join (if you needed data from table B on the records that did exist... like a count of records from B for each make/model 使用外部联接(如果您需要表B上确实存在的记录上的数据...像每个品牌/型号来自B的记录计数

SELECT A.vehicle_make, A.vehicle_model, count(B.*)
FROM tableA A
LEFT JOIN tableB B
 on A.vehicle_make = B.Vehicle_make
and A.vehicle_model = B.Vehicle_model
WHERE B.Vehicle_make is null
GROUP BY A.vehicle_make, A.vehicle_model

In works (unless you can have null values) In作品中(除非您可以具有空值)

select a.*
from TableA a
left outer join TableB b 
       on a.vehicle_make = b.vehicle_make 
      and a.vehicle_model = b.vehicle_model
where b.vehicle_make is null

暂无
暂无

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

相关问题 与两个表之间没有共同关系的表B列值相比,从表A中检索列 - Retrieve columns from table A in comparsion to table B column values where there is no common relationship between two tables 当公共列具有不同的名称但两个表中的信息相同时,如何连接两个sql表 - How to join two sql tables when the common column has different names but information is the same in both tables 如何将具有相同数据类型的两个不同列的结果放在一列中,以使两个表中的两行在目标表中都是唯一的 - How to put the result of two different columns with same data type within one column such that both rows from both tables are unique in target table 将两个不同的表与公共列上的公共第三个表连接起来 - Joining two different tables with a common third table on a common column SQL Server 2008,通过从不共享相同列信息的两个不同表中选择两个列来获取新表 - SQL server 2008, get a new table by selecting two columns from two different tables that do not share same column information 如何在 SQL 服务器的两个表中从另一个表中添加一个表中的列? - How to add a column in a table from another table having common id column in both the tables in SQL Server? 连接表A和表B,两者均具有column1 - Joining table A and table B, both having column1 as common SQL获取具有相同列A但不同B的记录 - SQL to get records with the same column A, but different B SQL查询...我需要从表1的同一列中插入2个值到表2的2个不同列中 - SQL query…I need to insert 2 values from same column of table1 to 2 different columns of table2 我如何连接两个表,其中两列引用 SQL 服务器中第二个表中的同一列 - How can i join two tables with two columns that refers the same column in the second table in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM