MySQL,给定一个列表,从表中选择丢失的行

发布于 2024-07-27 13:47:41 字数 546 浏览 3 评论 0原文

在过去的几分钟里,这让我发疯,

我有一个表,假设 table_alphabet 有三列。

letter(pri)  col1   col2   
a            24     55
b            45     45
c            23     44
...
y            33     55
z            45     22 

现在,在我的应用程序中,我有一个列表('a'、'b'、'lol'、'cats'、'z'、'foo')。

如果我这样做,

SELECT letter FROM table_alphabet WHERE letter IN ('a', 'b', 'lol', 'cats', 'z', 'foo')

我只会得到行,(a,b,z),

但我想要的是得到“lol”,“cats”,“foo”。 或者用英语来说,表格中缺少我列表中的哪些项目。

任何帮助将不胜感激,我今天似乎大脑出现故障。

This has been driving me crazy for the past few minutes

I have a table, lets say table_alphabet with three columns.

letter(pri)  col1   col2   
a            24     55
b            45     45
c            23     44
...
y            33     55
z            45     22 

Now in my application I have a list ('a', 'b', 'lol', 'cats', 'z', 'foo').

If I do

SELECT letter FROM table_alphabet WHERE letter IN ('a', 'b', 'lol', 'cats', 'z', 'foo')

I only get the rows, (a, b,z)

What I want though is to get 'lol', 'cats', 'foo'. Or in english, what items in my list are missing from the table.

Any help would be appreciated, I seem to be having a brain malfunction today.

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

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

发布评论

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

评论(3

我三岁 2024-08-03 13:47:41

抱歉,我第一次完全误解了这个问题。 您将需要一个子查询来将列表中的所有字母提取为行。 我能想到的唯一方法是将它们全部联合起来:

select let from (select 'a' as let union select 'b' as let union select 'lol' as let union select 'cats' as let union select 'z' as let union select 'foo' as let) as innerletters where let not in (select letter from table_alphabet);

所以换句话说,您的应用程序需要采用列表“('a', 'b', 'lol', 'cats', 'z', 'foo')" 并将其重写为 UNION:

(select 'a' as let union select 'b' as let union select 'lol' as let union select 'cats' as let union select 'z' as let union select 'foo' as let)

...并将其插入到查询中。 这应该返回列表中不存在于 table_alphabet 表的字母列中的字母:

+------+
| let  |
+------+
| lol  |
| cats |
| z    |
| foo  |
+------+

Sorry, I completely misread the question the first time through. You will need a subquery that can pull all of the letters in your list as rows. The only way I can think of doing this is by UNIONing them all together:

select let from (select 'a' as let union select 'b' as let union select 'lol' as let union select 'cats' as let union select 'z' as let union select 'foo' as let) as innerletters where let not in (select letter from table_alphabet);

So in other words, your application will need to take the list "('a', 'b', 'lol', 'cats', 'z', 'foo')" and rewrite it in a UNION:

(select 'a' as let union select 'b' as let union select 'lol' as let union select 'cats' as let union select 'z' as let union select 'foo' as let)

...and insert that into the query. This should return the letters from the list that are NOT present in the letter column of the table_alphabet table:

+------+
| let  |
+------+
| lol  |
| cats |
| z    |
| foo  |
+------+
想你的星星会说话 2024-08-03 13:47:41

我可能完全没有根据,但我认为仅靠 SQL 无法实现这一目标。

现在,如果您将列表加载到表中,将其命名为 table_list,然后将其左连接到 table_alphabet 并排除 Alphabet 为空的所有行,那么您可以获得排除列表。

I may be completely off base, but I dont think you are going to achieve that in just SQL alone.

Now, if you loaded your list into a table, call it table_list, then left join it to your table_alphabet and exclude all rows where alphabet is null then you could get your list of exclusions.

二货你真萌 2024-08-03 13:47:41

即使您正在过滤的非静态列表,NOT IN 命令也将起作用。 您可以使用子查询,例如:

select letter from table_alphabet where letter NOT IN ( select letter from exclude_table )

The NOT IN command will work even with a non static list you are filtering against. You can use a sub-query like:

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