未从列表中返回的项目

发布于 2025-01-04 12:59:38 字数 174 浏览 1 评论 0原文

我有一个 860 项长的项目列表。当我执行查询时: select * from tableA where item in (... items ...) 我得到 858 个项目。我想知道列表中的 2 项不在表 A 中。

NOT 返回表中不在列表中的所有项目,我想要列表中不在表中的所有项目。

I have a list of items that is 860 items long. When i execute the query: select * from tableA where item in (... items ...) I get 858 items. I would like to know the 2 items in the list that are not in tableA.

NOT returns all of the items in the table that are not in the list, I want all the items in the list that are not in the table.

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

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

发布评论

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

评论(2

何时共饮酒 2025-01-11 12:59:39

根据我对这个问题的最初理解,我建议只添加关键字 NOT

SELECT * FROM tableA WHERE item NOT IN (... items ...)

但根据评论,上面不会返回你想要的内容。原始问题经过编辑以包含此新信息。

因此,您需要将 WHERE 子句中的数据转换为可查询的形式。这是一种方法,我创建一个名为“items”的附加表,并使用 INSERT 语句将每个项目放入此项目表中。由于我无权访问您的数据,因此我将使用整数作为项目并使用较少量的数据进行设置。

--Set up some sample data
CREATE TABLE tableA(item INT PRIMARY KEY)

INSERT INTO tableA SELECT 1
INSERT INTO tableA SELECT 2
INSERT INTO tableA SELECT 3
INSERT INTO tableA SELECT 4
INSERT INTO tableA SELECT 9
INSERT INTO tableA SELECT 10

SELECT * FROM tableA WHERE item IN (0,1,2,3,4,5,6)
SELECT * FROM tableA WHERE item NOT IN (0,1,2,3,4,5,6)

-- Create a table and insert all the 860 items from your where clause
CREATE TABLE items(item INT)
INSERT INTO items SELECT 0
INSERT INTO items SELECT 1
INSERT INTO items SELECT 2
INSERT INTO items SELECT 3
INSERT INTO items SELECT 4
INSERT INTO items SELECT 5
INSERT INTO items SELECT 6

-- Want to find a query that returns all of the items in the newly created items table
-- that are not in the original tableA (in this example, the values returned are 0,5,6)
SELECT * FROM items WHERE item NOT IN (SELECT item FROM tableA)

Based on my original understanding of the question, I suggested to just add the keyword NOT

SELECT * FROM tableA WHERE item NOT IN (... items ...)

But as per the comment the above will not return what you want. The original question was edited to include this new infomration.

So, you need to get your data from your WHERE clause into a form that is queryable. Here is one way to do it where I create an additional table named "items" and have INSERT statements to place each item into this items table. Since I do not have access to your data, I am going to use integers for the items and set it up with a smaller amount of data.

--Set up some sample data
CREATE TABLE tableA(item INT PRIMARY KEY)

INSERT INTO tableA SELECT 1
INSERT INTO tableA SELECT 2
INSERT INTO tableA SELECT 3
INSERT INTO tableA SELECT 4
INSERT INTO tableA SELECT 9
INSERT INTO tableA SELECT 10

SELECT * FROM tableA WHERE item IN (0,1,2,3,4,5,6)
SELECT * FROM tableA WHERE item NOT IN (0,1,2,3,4,5,6)

-- Create a table and insert all the 860 items from your where clause
CREATE TABLE items(item INT)
INSERT INTO items SELECT 0
INSERT INTO items SELECT 1
INSERT INTO items SELECT 2
INSERT INTO items SELECT 3
INSERT INTO items SELECT 4
INSERT INTO items SELECT 5
INSERT INTO items SELECT 6

-- Want to find a query that returns all of the items in the newly created items table
-- that are not in the original tableA (in this example, the values returned are 0,5,6)
SELECT * FROM items WHERE item NOT IN (SELECT item FROM tableA)
帅的被狗咬 2025-01-11 12:59:38

我建议您将列表转换为临时表(有大量的 udf 可供您使用,例如: http://blog.sqlauthority.com/2007/05/06/sql-server-udf-function-to-convert-list-to-table/

一旦你有了临时表#List,您可以执行以下操作;

CREATE TABLE #List
(
  [ListItem] INT
)

SELECT
    *
FROM
    #List AS l
LEFT OUTER JOIN
    tableA AS t
ON
    t.[Item] = l.[ListItem]
WHERE
    t.[Item] IS NULL

查看实际操作: https://data .stackexchange.com/stackoverflow/query/61259/items-not-returned-from-a-list

I would recommend that you convert your list into a temp table (there are a ton of udfs floating around that you can use ex: http://blog.sqlauthority.com/2007/05/06/sql-server-udf-function-to-convert-list-to-table/)

Once you have your temp table #List, you can do the following;

CREATE TABLE #List
(
  [ListItem] INT
)

SELECT
    *
FROM
    #List AS l
LEFT OUTER JOIN
    tableA AS t
ON
    t.[Item] = l.[ListItem]
WHERE
    t.[Item] IS NULL

See it in action: https://data.stackexchange.com/stackoverflow/query/61259/items-not-returned-from-a-list

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文