gpt4 book ai didi

mysql - 从表中选择 *,其中 id != long_list

转载 作者:行者123 更新时间:2023-11-29 08:23:07 24 4
gpt4 key购买 nike

注意:我正在使用 SQLAlchemy,但我选择将我的想法转化为常规 SQL,以便更多人可以回答/阅读这个问题。

我正在尝试选择表中不在过滤器中的所有条目。我通过将 user_id 与 table_id 关联来获取过滤器。我目前有一个正在使用的实现,

从cleared_table中选择*,其中user_id=user_id

然后我构建第二个选择语句,

select * from table where table_id != first_id and table_id != second_id ....

我正在寻找一种方法将其压缩为单个 SQL 语句,而不必两次访问数据库。

示例clear_table,

cleared_table
user_id | table_id
1 | 1
1 | 2
1 | 3
2 | 2
3 | 3

最佳答案

您正在寻找嵌套查询。

select * from table where table_id not in (select table_id from cleared_table where user_id=user_id)

关于mysql - 从表中选择 *,其中 id != long_list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18752790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com