如何根据记录可能共享值的多个字段排除记录

发布于 2025-01-25 21:09:56 字数 1945 浏览 2 评论 0 原文

我有两个地址表。它们的格式非常相似,[主]表也具有客户名称

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 表为每个主要客户提供许多地址,并且许多次主要地址也列为 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 |

我希望它能返回ID 2-5,但它仅返回#5,因为它与Shipto的主要元素相匹配,但我需要它查看整体记录。

我还尝试添加一个和括号的想法认为它会起作用,但这也不是。

SELECT * 
FROM [shipTo]  
WHERE [custID] = @tmpCustID
  AND ([Address 1] <> @chkAddress1
  AND [Address 2] <> @chkAddress2
  AND [City] <> @chkCity
  AND [State] <> @chkState
  AND [Zip] <> @chkZip)

I have two tables of addresses. They are formatted very similarly, to [main] table also has the customer name

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)
)

The shipto table has many addresses for each main customer, and many times the main addresses is also listed as a shipto. I do not want to include that main address in my results but I can't get it to work correctly since in many cases the other addresses may share a city or state, or even a blank address2.

Here is what I have...

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

Here is the data:

MAIN

|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 |

I am expecting it to return id 2-5, but it's only returning #5 because it's matching some element of the main address to the shipto, but I need it to see the record as a whole.

I also tried adding an AND and parenthesis thinking that it would work, but that didn't either.

SELECT * 
FROM [shipTo]  
WHERE [custID] = @tmpCustID
  AND ([Address 1] <> @chkAddress1
  AND [Address 2] <> @chkAddress2
  AND [City] <> @chkCity
  AND [State] <> @chkState
  AND [Zip] <> @chkZip)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

番薯 2025-02-01 21:09:56

也许尝试此查询

SELECT * FROM [shipTo]  
WHERE [custID] = @tmpCustID
AND NOT
([Address 1] = @chkAddress1
 AND [Address 2] = @chkAddress2
 AND [City] = @chkCity
 AND [State] = @chkState
 AND [Zip] = @chkZip)

Maybe try this query

SELECT * FROM [shipTo]  
WHERE [custID] = @tmpCustID
AND NOT
([Address 1] = @chkAddress1
 AND [Address 2] = @chkAddress2
 AND [City] = @chkCity
 AND [State] = @chkState
 AND [Zip] = @chkZip)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文