如何排除基于记录可能共享值的多个字段的记录
How to Exclude Record Based on Multiple Fields where Records May Share Values
我有两个 table 地址。它们的格式非常相似,[main] table 也有客户名称
CREATE TABLE [shipTo]
(
[id] int IDENTITY(1,1),
[custID] int,
[address1] varchar(255),
[address2] varchar(255),
[city] varchar(255),
[state] varchar(255),
[zip] varchar(255)
)
shipto
table每个主要客户都有很多地址,很多时候主要地址也列为一个shipto
。我不想在我的结果中包含那个主要地址,但我无法让它正常工作,因为在许多情况下,其他地址可能共享一个城市或州,甚至是一个空白地址2。
这是我的...
SELECT
@tmpCustID = [id],
@chkAddress1 = [Address 1],
@chkAddress2 = [Address 2],
@chkCity = [City],
@chkState = [State],
@chkZip = [Zip]
FROM
[main]
WHERE
[id] = @cnt
SELECT *
FROM [shipTo]
WHERE [custID] = @tmpCustID
AND [Address 1] <> @chkAddress1
AND [Address 2] <> @chkAddress2
AND [City] <> @chkCity
AND [State] <> @chkState
AND [Zip] <> @chkZip
这是数据:
主要
|id| address1 | address2 | city | state | zip |
|2 | 123 baker st | | columbus | oh | 43081 |
SHIPTO
|id| custid| address1 | address2 | city | state | zip |
|1 | 2 | 123 baker st | | columbus | oh | 43081 |
|2 | 2 | 626 oak point | | cleveland | oh | 43092 |
|3 | 2 | 17 purple blvd | | columbus | ga | 81265 |
|4 | 2 | 851 brawny | | grand rapids | mi | 49417 |
|5 | 2 | 101 olive rd | suite #3 | grand rapids | mi | 49417 |
我希望它是 return id 2-5,但它只是 returning #5 因为它正在将主要地址的某些元素与 shipto 匹配,但我需要它才能看到整个记录。
我还尝试添加一个 AND 和括号,认为它可以工作,但也没有。
SELECT *
FROM [shipTo]
WHERE [custID] = @tmpCustID
AND ([Address 1] <> @chkAddress1
AND [Address 2] <> @chkAddress2
AND [City] <> @chkCity
AND [State] <> @chkState
AND [Zip] <> @chkZip)
也许试试这个查询
SELECT * FROM [shipTo]
WHERE [custID] = @tmpCustID
AND NOT
([Address 1] = @chkAddress1
AND [Address 2] = @chkAddress2
AND [City] = @chkCity
AND [State] = @chkState
AND [Zip] = @chkZip)
我有两个 table 地址。它们的格式非常相似,[main] table 也有客户名称
CREATE TABLE [shipTo]
(
[id] int IDENTITY(1,1),
[custID] int,
[address1] varchar(255),
[address2] varchar(255),
[city] varchar(255),
[state] varchar(255),
[zip] varchar(255)
)
shipto
table每个主要客户都有很多地址,很多时候主要地址也列为一个shipto
。我不想在我的结果中包含那个主要地址,但我无法让它正常工作,因为在许多情况下,其他地址可能共享一个城市或州,甚至是一个空白地址2。
这是我的...
SELECT
@tmpCustID = [id],
@chkAddress1 = [Address 1],
@chkAddress2 = [Address 2],
@chkCity = [City],
@chkState = [State],
@chkZip = [Zip]
FROM
[main]
WHERE
[id] = @cnt
SELECT *
FROM [shipTo]
WHERE [custID] = @tmpCustID
AND [Address 1] <> @chkAddress1
AND [Address 2] <> @chkAddress2
AND [City] <> @chkCity
AND [State] <> @chkState
AND [Zip] <> @chkZip
这是数据:
主要
|id| address1 | address2 | city | state | zip |
|2 | 123 baker st | | columbus | oh | 43081 |
SHIPTO
|id| custid| address1 | address2 | city | state | zip |
|1 | 2 | 123 baker st | | columbus | oh | 43081 |
|2 | 2 | 626 oak point | | cleveland | oh | 43092 |
|3 | 2 | 17 purple blvd | | columbus | ga | 81265 |
|4 | 2 | 851 brawny | | grand rapids | mi | 49417 |
|5 | 2 | 101 olive rd | suite #3 | grand rapids | mi | 49417 |
我希望它是 return id 2-5,但它只是 returning #5 因为它正在将主要地址的某些元素与 shipto 匹配,但我需要它才能看到整个记录。
我还尝试添加一个 AND 和括号,认为它可以工作,但也没有。
SELECT *
FROM [shipTo]
WHERE [custID] = @tmpCustID
AND ([Address 1] <> @chkAddress1
AND [Address 2] <> @chkAddress2
AND [City] <> @chkCity
AND [State] <> @chkState
AND [Zip] <> @chkZip)
也许试试这个查询
SELECT * FROM [shipTo]
WHERE [custID] = @tmpCustID
AND NOT
([Address 1] = @chkAddress1
AND [Address 2] = @chkAddress2
AND [City] = @chkCity
AND [State] = @chkState
AND [Zip] = @chkZip)