问题描述
基本上我要检查一个MySQL文本列,其中包含逗号分隔值是否包含任何包含在数组中的值。我知道这可以用一个环和多个查询来完成,但我希望为单个查询。这可能吗?谢谢你。
我会用一个解决方案是这样的:
SELECT
*
从
yourtable
哪里
海峡RLIKE
CONCAT('[[:其中;:]]',REPLACE('值,在中,,阵列,,,[[:>:]] | [[:其中;:]]'), '[[:计算值:]]')
这会使下面的字符串:
的价值观,在,在,阵'
是这样的:
<$p$p><$c$c>[[:<:]]values[[:>:]]|[[:<:]]in[[:>:]]|[[:<:]]the[[:>:]]|[[:<:]]array[[:>:]] [:&LT;:]]
和 [:&GT;:]]
字界限如此它只会匹配整个单词,|是一种左右,它会匹配任何的话。请参阅小提琴 rel=\"nofollow\">这里。
Basically I want to check whether a mysql text column, which contains comma-separated values, contains any of the values contained in an array. I know this can be done with a loop and multiple queries, but I was hoping for a single query. Is this possible? Thank you.
I would use a solution like this:
SELECT
*
FROM
yourtable
WHERE
str RLIKE
CONCAT('[[:<:]]', REPLACE('values,in,the,array', ',', '[[:>:]]|[[:<:]]'), '[[:>:]]')
this will make the following string:
'values,in,the,array'
like this:
[[:<:]]values[[:>:]]|[[:<:]]in[[:>:]]|[[:<:]]the[[:>:]]|[[:<:]]array[[:>:]]
[[:<:]]
and [[:>:]]
are word boundaries so it will match only whole words, and | is an OR so it will match any of the words. Please see fiddle here.
这篇关于可能与一个MySQL查询? &QUOT;列包含任何数组的价值观和QUOT的;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!