如何在合并数据集之前检查行是否存在(SQL 服务器)
How to check for row existence to before merging datasets (SQL Server)
我正在尝试合并两个数据集,并想检查其中一个表中是否已存在一行。
这里我想 select table_1
中尚未在 table_2
上匹配 3 列值的所有内容。
Table 1
| id | key | value |
|----|-------|---------|
| 1 | brand | alpha |
| 1 | color | red |
| 2 | brand | charlie |
Table 2
| id | key | value |
|----|-------|---------|
| 2 | brand | charlie |
| 2 | color | yellow |
期望输出
| id | key | value |
|----|-------|---------|
| 2 | color | yellow |
查询
select [id], [key], [value]
from db..table_1
where not exists (
select
[id], [key], [value]
from db..table_2
)
如何在 SQL 服务器中实现此目的?
有很多方法,但我想这个答案必须更改,因为根据列名我认为您的要求不明确。这是三种不同的方法,但输出可能会有所不同,例如,t1 中有重复项已经存在。
-- except
SELECT [id], [key], [value] FROM dbo.table_1
EXCEPT
SELECT [id], [key], [value] FROM dbo.table_2;
-- not exists (must be correlated to t1!)
SELECT [id], [key], [value]
FROM dbo.table_1 AS t1
WHERE NOT EXISTS
(
SELECT 1 FROM dbo.table_2 AS t2
WHERE t2.[id] = t1.[id]
AND t2.[key] = t1.[key]
AND t2.[value] = t1.[value]
);
-- left outer join
SELECT t1.[id], t1.[key], t1.[value]
FROM dbo.table_1 AS t1
LEFT OUTER JOIN dbo.table_2 AS t2
ON t2.[id] = t1.[id]
AND t2.[key] = t1.[key]
AND t2.[value] = t1.[value]
WHERE t2.[id] IS NULL;
如果表之间有不同的排序规则,您可以使用 COLLATE
子句解决冲突,但您需要确保映射到正确的排序规则以按照您期望的方式比较值(保持在请注意,如果您映射到区分大小写或二进制排序规则,banana
<> BANANA
).
CREATE TABLE dbo.t1(a nvarchar(32) COLLATE Slovenian_BIN);
CREATE TABLE dbo.t2(a nvarchar(32) COLLATE Latin1_General_CS_AS_KS_WS);
SELECT a FROM dbo.t1
EXCEPT
SELECT a COLLATE Slovenian_BIN FROM dbo.t2;
---------------------
我正在尝试合并两个数据集,并想检查其中一个表中是否已存在一行。
这里我想 select table_1
中尚未在 table_2
上匹配 3 列值的所有内容。
Table 1
| id | key | value |
|----|-------|---------|
| 1 | brand | alpha |
| 1 | color | red |
| 2 | brand | charlie |
Table 2
| id | key | value |
|----|-------|---------|
| 2 | brand | charlie |
| 2 | color | yellow |
期望输出
| id | key | value |
|----|-------|---------|
| 2 | color | yellow |
查询
select [id], [key], [value]
from db..table_1
where not exists (
select
[id], [key], [value]
from db..table_2
)
如何在 SQL 服务器中实现此目的?
有很多方法,但我想这个答案必须更改,因为根据列名我认为您的要求不明确。这是三种不同的方法,但输出可能会有所不同,例如,t1 中有重复项已经存在。
-- except
SELECT [id], [key], [value] FROM dbo.table_1
EXCEPT
SELECT [id], [key], [value] FROM dbo.table_2;
-- not exists (must be correlated to t1!)
SELECT [id], [key], [value]
FROM dbo.table_1 AS t1
WHERE NOT EXISTS
(
SELECT 1 FROM dbo.table_2 AS t2
WHERE t2.[id] = t1.[id]
AND t2.[key] = t1.[key]
AND t2.[value] = t1.[value]
);
-- left outer join
SELECT t1.[id], t1.[key], t1.[value]
FROM dbo.table_1 AS t1
LEFT OUTER JOIN dbo.table_2 AS t2
ON t2.[id] = t1.[id]
AND t2.[key] = t1.[key]
AND t2.[value] = t1.[value]
WHERE t2.[id] IS NULL;
如果表之间有不同的排序规则,您可以使用 COLLATE
子句解决冲突,但您需要确保映射到正确的排序规则以按照您期望的方式比较值(保持在请注意,如果您映射到区分大小写或二进制排序规则,banana
<> BANANA
).
CREATE TABLE dbo.t1(a nvarchar(32) COLLATE Slovenian_BIN);
CREATE TABLE dbo.t2(a nvarchar(32) COLLATE Latin1_General_CS_AS_KS_WS);
SELECT a FROM dbo.t1
EXCEPT
SELECT a COLLATE Slovenian_BIN FROM dbo.t2;
---------------------