gpt4 book ai didi

mysql - 在 MySQL 中获取不存在于另一个表的两列中的行

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

MySQL 中有两个表,详细信息如下:

表 1:

+-------------+---------------+
| user_id | isactive |
+-------------+---------------+
| aaa | 0 |
+-------------+---------------+
| bbb | 0 |
+-------------+---------------+

表 2:

 +-------------+---------------+-----------+
|store_no | owner | store |
+-------------+---------------+-----------+
|1234 | aaa | aaa,xyz |
+-------------+---------------+-----------+
|1006 | aaa | aaa |
+-------------+---------------+-----------+
|1005 | ccc | www |
+-------------+---------------+-----------+

我需要从表 1 中获取条目既不在表 2 的“owner”列中也不在“store”列中的行。例如,在上面的场景中,结果集应该包含“bbb”。我尝试使用 find_in_set、locate 等,但无法按要求获取详细信息。请帮助..

更新表格格式

查询:

select a.user_id from table1 u
left outer join table2 a
on (owner=user_id or concat(',',store,',') like concat('%,',user_id,',%'))
where (find_in_set(user_id,owner) = 0 or find_in_set(user_id,store) = 0)
and isactive=0

仅供引用,商店列可以具有多个用户 ID 的串联值

最佳答案

您可以尝试使用NOT EXISTS

SELECT 
T1.user_id
FROM TABLE_1 T1
WHERE NOT EXISTS (
SELECT 1
FROM
TABLE_2 T2
WHERE T2.owner = T1.user_id OR FIND_IN_SET(T1.user_id,T2.store) > 0
);

SQL FIDDLE DEMO

建议:

Is storing a delimited list in a database column really that bad?

关于mysql - 在 MySQL 中获取不存在于另一个表的两列中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38053338/

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