首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找字符串是否包含在同一mysql表的列中

查找字符串是否包含在同一mysql表的列中
EN

Stack Overflow用户
提问于 2019-10-14 02:39:28
回答 1查看 20关注 0票数 0

我需要从一个表中获取结果,在这个表中,一条记录被设置为不排除它,或者在满足条件的情况下排除它。我在这里搜索了解决方案,并提出了以下代码,但查询总是返回这两条记录。这是包含两个条目的基本表。我期望查询的工作方式是返回第一条记录,因为apply_method是0,而不返回第二条记录,因为apply_method是1,而7在restrict_to列中。有人能解释一下我漏掉了什么吗?

代码语言:javascript
复制
   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) 
  )
)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-14 02:53:24

要处理逗号分隔的值字符串,请使用带有NOTMySQL's string function FIND_IN_SET()

代码语言:javascript
复制
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

代码语言:javascript
复制
-- 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在索引列上联接,而不是对每一行进行字符串搜索。在一张大桌子上它可能会快得多。

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58366661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档