-1

SQL 查询是这样的:

SELECT * from ActiveListing a WHERE [seller-id] in 
(SELECT id from Product p where PurchaserID = 60741 and right(id, 4) = '-XRU')

第 2 条 Select 语句的结果 if run 返回 1000 个 id 将被第 1 条 Select 语句使用,如果运行返回 800 条记录,这是正确的。

问题:

我将如何更改此查询以获得没有结果的 200 个 ID

RDMS:MS SQL

样本:

 SELECT id from Product p where PurchaserID = 60741 and right(id, 4) = '-XRU'

返回 ID (1,2,3,4)

 SELECT * from ActiveListing a WHERE [seller-id] in (1,2,3,4) 

返回两个记录 id 1 和 2 返回一个值 id 3 和 4 不返回任何内容

我想得到 id 的 3 和 4

我希望我的问题更清楚一点,我感谢所有试图提供帮助的人。我仍在测试其他建议

4

4 回答 4

4

有点不清楚 - 但请NOT IN尝试

SELECT * from ActiveListing a WHERE [seller-id] not in 
(SELECT id from Product p where PurchaserID = 60741 and right(id, 4) = '-XRU')
于 2013-06-13T20:00:24.610 回答
2

正如兰迪建议的那样,您可以更改INNOT IN. 但是,我鼓励您开始考虑 JOIN 和EXISTS(半连接):

SELECT
   A.* -- try to use explicit columns though instead of *
FROM
   dbo.ActiveListing A
WHERE
   Right(A.[seller-id], 4) <> '-XRU' --may improve performance
   AND NOT EXISTS ( -- this is actually an anti-semi-join
      SELECT *
      FROM dbo.Product P
      WHERE
         P.PurchaserID = 60741
         AND Right(P.id, 4) = '-XRU'
         AND A.[seller-id] = P.id
   )
;

SELECT
   A.* -- try to use explicit columns though instead of *
FROM
   dbo.ActiveListing A
   LEFT JOIN dbo.Product P
      ON P.PurchaserID = 60741
      AND Right(P.id, 4) = '-XRU'
      AND A.[seller-id] = P.id
WHERE
   Right(A.[seller-id], 4) <> '-XRU' --may improve performance
   AND P.id IS NULL
;

请注意我添加的“可能会提高性能”也消除了任何NULL值,因此如果A.[seller-id]可以,请进行相应调整NULL

如果我向后理解了这一点,并且您希望其中的行Product不在我们中,ActiveListing我们只需翻转表格和条件:

SELECT
   P.* -- try to use explicit columns though instead of *
FROM
   dbo.Product P
WHERE
   P.PurchaserID = 60741
   AND Right(P.id, 4) = '-XRU'
   AND NOT EXISTS ( -- this is actually an anti-semi-join
      SELECT *
      FROM dbo.ActiveListing A
      WHERE P.id = A.[seller-id]
   )
;

SELECT
   P.* -- try to use explicit columns though instead of *
FROM
   dbo.Product P
   LEFT JOIN dbo.ActiveListing A
      ON P.id = A.[seller-id]
WHERE
   P.PurchaserID = 60741
   AND Right(P.id, 4) = '-XRU'
   AND A.[seller-id] IS NULL
;
于 2013-06-13T20:05:20.360 回答
1

如果我对您的理解正确,您希望在产品中找到符合条件的条目

1) id 的最后 4 个字符是 -XRU 2) PurchaserID = 60741 3) 没有与该 ID 匹配的 ActiveListing

那我建议

SELECT p.Id
FROM Product as p
WHERE p.PurchaserID = 60741
  AND RIGHT(p.id,4) = '-XRU'
  AND NOT EXISTS (
    SELECT 1
    FROM ActiveListing AS a
    WHERE a.seller-id = p.id
    )

尽管您也可以将 NOT IN 用于 NOT EXISTS 子句(但我认为这不是声明性的),甚至可以使用 LEFT JOIN 并检查 NULLS(但这又不是声明性的)

输出ID

SELECT p.Id AS [CheckID]
FROM Product as p
WHERE p.PurchaserID = 60741

输出不同的 Id

SELECT DISTINCT p.Id
FROM Product as p
WHERE p.PurchaserID = 60741

检查行数是否相同——如果不是,则可能是 NO ROW 在 ActiveListing 中没有对应的条目

备用查询性能更差的版本——但必须使用 Vanilla SQL

SELECT p.Id AS [CheckID]
FROM Product as p
WHERE p.PurchaserID = 60741

EXECPT
SELECT seller-id AS [checkID] FROM ActiveListing

从逻辑上讲,这里没有任何问题 - 如果您运行查询并且没有得到任何结果,那么......没有结果......

于 2013-06-13T20:10:37.363 回答
0

我相信您会将 IN 更改为 NOT IN。

于 2013-06-13T20:01:08.013 回答