简体   繁体   English

mysql find_in_set字符串开头

[英]mysql find_in_set string starting with

I need to search value starting with given string in comma separated values. 我需要搜索以逗号分隔值的给定字符串开头的值。
Example, 例,
I have '1_2_3_4_5_6, 1_2_3_4_5_8, 1_2_3_6_5_8' in my column. 我的栏中有“ 1_2_3_4_5_6、1_2_3_4_5_8、1_2_3_6_5_8”。 I can search for rows with exact value using 我可以使用以下方式搜索具有确切值的行
select * from table where find_in_set('1_2_3_4_5_6',column)

But how to search, if starting part of the string is given? 但是,如果给出字符串的开始部分,该如何搜索呢? something like this: 像这样的东西:
select * from table where find_in_set('1_2_3%',column) ? select * from table where find_in_set('1_2_3%',column)吗?

If I understand you correctly (I'm still not sure I do), you could just use: 如果我正确理解了您(我仍然不确定我是否知道),则可以使用:

SELECT * FROM table WHERE column LIKE '%1_2_3%';

This would give you columns where the value is like: 这将为您提供值如下的列:

1_2_3_4_5_5
1_4_5_, 1_2_3_4_5_, 6_7

and so on. 等等。

But you should really normalize your tables. 但是您应该真正规范化您的表。 This is important for good queries and performance wise also important. 这对于良好的查询很重要,而在性能方面也很重要。

According to @Xatenev suggestions, if you really like only the values and the row of each matching row, this won't work so well and will be a lot of overhead. 根据@Xatenev的建议,如果您真的只喜欢值和每个匹配行的行,那么它将无法很好地工作,并且会产生大量开销。 This are the steps that I would perform: 这是我要执行的步骤:

  1. Split all CSV columns into multiple rows (this is a hack and a performance killer, I found some working solution but did not test it, see here ): Pseudo Code: SELECT ID, SPLIT(',', column) AS entries FROM table (NOT WORKING) 将所有CSV列拆分为多行(这是黑客和性能杀手,我找到了一些可行的解决方案,但未对其进行测试,请参阅此处 ):伪代码: SELECT ID, SPLIT(',', column) AS entries FROM table (不工作)
  2. Filter the new virtual table to select only rows that match the prefix ( SELECT * FROM virtual_table WHERE find_in_set("1_2_3%, entries) ORDER BY ID ) 过滤新的虚拟表以仅选择与前缀匹配的行( SELECT * FROM virtual_table WHERE find_in_set("1_2_3%, entries) ORDER BY ID
  3. Concatenate the matching entries back into a list for each ID . 将匹配entries串联回每个ID的列表中。 eg SELECT ID, GROUP_CONCAT(entries SEPARATOR ', ') FROM filtered_table GROUP BY ID 例如SELECT ID, GROUP_CONCAT(entries SEPARATOR ', ') FROM filtered_table GROUP BY ID
  4. Do something 做一点事

The unknown part is the beginning with the split in multiple rows. 未知部分是从多行拆分开始的部分。 There are a lot of possible solutions all with their own drawbacks or advantages. 有许多可能的解决方案都有其自身的缺点或优点。 Be aware that this will always (regardless of the selected method) will cost a lot of performance. 请注意,这将始终(无论选择哪种方法)都会花费很多性能。

ADDITIONAL NODE: 其他节点:

It could be adventures in your situation, that you get each row matching your search string like in my first example and filter them in memory. 在您所处的环境中可能会很冒险,就像我的第一个示例一样,使每一行都与您的搜索字符串匹配并将其过滤到内存中。 This might be faster than doing this in MYSQL. 这可能比在MySQL中执行此操作更快。

you can try with 'REGEXP' 您可以尝试使用“ REGEXP”

If you want to match data with subtring, please try this 如果要用subtring匹配数据,请尝试此操作

SELECT * FROM `table` WHERE `column` REGEXP '[, ]?1_2_3[^,]*,?' ;

Or 要么

If you want to exact start match, please try this 如果要精确开始比赛,请尝试此操作

SELECT * FROM `table` WHERE `column` REGEXP '[, ]?1_2_3[^,]*,?' AND `column` NOT REGEXP '[^, ]1_2_3[^,]*,?' ;

I was able to solve it with no-regex. 我能够用无正则表达式解决它。 Sonam's Answer is correct as well. 索南的答案也是正确的。

SELECT * from table WHERE CONCAT(',', columnname, ',') like '%,1_2_3%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM