我需要从一个表中获取结果,在这个表中,一条记录被设置为不排除它,或者在满足条件的情况下排除它。我在这里搜索了解决方案,并提出了以下代码,但查询总是返回这两条记录。这是包含两个条目的基本表。我期望查询的工作方式是返回第一条记录,因为apply_method是0,而不返回第二条记录,因为apply_method是1,而7在restrict_to列中。有人能解释一下我漏掉了什么吗?
table (
name text,
apply_method int,
restrict_to text
)
name ='name1',
apply_method = 0
restrict_to = '1,2,3,4,5,6,7'
name ='name2',
apply_method = 1
restrict_to = '1,2,3,4,5,6,7'
select
name
from table
where (
apply_method='0' or (
apply_method='1'
and '7' not in (select restrict_to from table)
)
)发布于 2019-10-14 02:53:24
要处理逗号分隔的值字符串,请使用带有NOT的MySQL's string function FIND_IN_SET()。
SELECT name
FROM t
WHERE apply_method = 0
OR (apply_method = 1 AND NOT FIND_IN_SET(7, restrict_to))Here it is in action
理想情况下,restrict_to值应该在一个单独的表中表示,并且与表的主键具有一对多的关系。随着表的增长,FIND_IN_SET()的性能可能不够好。
考虑创建第二个只有两列的表。使用任何真实的列作为示例表的主键,而不是name。
-- table restrictions
name, restrict_to
name1, 1
name1, 2
name1, 3
name1, 4
name1, 5
name1, 6
name1, 7
name2, 1
name2, 2
name2, 3
name2, 4
name2, 5
name2, 6
name2, 7并使用LEFT JOIN查询它。这将允许MySQL在索引列上联接,而不是对每一行进行字符串搜索。在一张大桌子上它可能会快得多。
SELECT t.name
FROM
table t
-- Join on the common column and the value you want to restrict
LEFT JOIN restrictions ON t.name = restrictions.name AND restrict_to = 7
WHERE
apply_method = 0
-- A NULL in the left join indicates a non-existing row
OR (apply_method = 1 AND restrictions.name IS NULL)https://stackoverflow.com/questions/58366661
复制相似问题