简体   繁体   English

MySQL find_in_set 与多个搜索字符串

[英]MySQL find_in_set with multiple search string

I find that find_in_set only search by a single string:-我发现find_in_set只搜索单个字符串:-

find_in_set('a', 'a,b,c,d')

In the above example, 'a' is the only string used for search.在上面的示例中,“a”是唯一用于搜索的字符串。

Is there any way to use find_in_set kind of functionality and search by multiple strings, like:-有什么方法可以使用 find_in_set 类型的功能并通过多个字符串进行搜索,例如:-

find_in_set('a,b,c', 'a,b,c,d')

In the above example, I want to search by three strings 'a,b,c'.在上面的示例中,我想通过三个字符串 'a,b,c' 进行搜索。

One way I see is using OR我看到的一种方法是使用OR

find_in_set('a', 'a,b,c,d') OR find_in_set('b', 'a,b,c,d') OR find_in_set('b', 'a,b,c,d')

Is there any other way than this?除了这个还有其他方法吗?

没有本机功能可以做到这一点,但您可以使用以下技巧实现您的目标

WHERE CONCAT(",", `setcolumn`, ",") REGEXP ",(val1|val2|val3),"

The MySQL function find_in_set() can search only for one string in a set of strings. MySQL 函数find_in_set()只能搜索一组字符串中的一个字符串。

The first argument is a string, so there is no way to make it parse your comma separated string into strings (you can't use commas in SET elements at all!).第一个参数是一个字符串,因此无法将逗号分隔的字符串解析为字符串(您根本不能在 SET 元素中使用逗号!)。 The second argument is a SET, which in turn is represented by a comma separated string hence your wish to find_in_set('a,b,c', 'a,b,c,d') which works fine, but it surely can't find a string 'a,b,c' in any SET by definition - it contains commas.第二个参数是一个 SET,它又由逗号分隔的字符串表示,因此您希望find_in_set('a,b,c', 'a,b,c,d')工作正常,但它肯定可以' t 根据定义在任何 SET 中找到字符串'a,b,c' - 它包含逗号。

You can also use this custom function您也可以使用此自定义功能

CREATE FUNCTION SPLIT_STR(
  x VARCHAR(255),
  delim VARCHAR(12),
  pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, ''); 

DELIMITER $$
    CREATE FUNCTION `FIND_SET_EQUALS`(`s1` VARCHAR(200), `s2`  VARCHAR(200)) 
    RETURNS TINYINT(1)
    LANGUAGE SQL
    BEGIN
          DECLARE a INT Default 0 ;
            DECLARE isEquals TINYINT(1) Default 0 ;
          DECLARE str VARCHAR(255);
          IF s1 IS NOT NULL AND s2 IS NOT NULL THEN
             simple_loop: LOOP
                 SET a=a+1;
                 SET str= SPLIT_STR(s2,",",a);
                 IF str='' THEN
                    LEAVE simple_loop;
                 END IF;
                 #Do  check is in set
                 IF FIND_IN_SET(str, s1)=0 THEN
                    SET isEquals=0;
                     LEAVE simple_loop;
                 END IF;
                 SET isEquals=1;
            END LOOP simple_loop;
          END IF;
        RETURN isEquals;
    END;
    $$
    DELIMITER ;

SELECT FIND_SET_EQUALS('a,c,b', 'a,b,c')- 1
SELECT FIND_SET_EQUALS('a,c', 'a,b,c')- 0
SELECT FIND_SET_EQUALS(null, 'a,b,c')- 0

Amazing answer by @Pavel Perminov! @Pavel Perminov 的惊人回答! - And also nice comment by @doru for dynamically check.. - 还有@doru 对动态检查的好评论..

From there what I have made for PHP code CONCAT(',','" . $country_lang_id . "', ',') REGEXP CONCAT(',(', REPLACE(YourColumnName, ',', '|'), '),') this below query may be useful for someone who is looking for ready code for PHP.从那里我为 PHP 代码CONCAT(',','" . $country_lang_id . "', ',') REGEXP CONCAT(',(', REPLACE(YourColumnName, ',', '|'), '),')下面的查询可能对正在寻找 PHP 代码的人有用。

$country_lang_id = "1,2";

$sql = "select a.* from tablename a where CONCAT(',','" . $country_lang_id . "', ',') REGEXP CONCAT(',(', REPLACE(a.country_lang_id, ',', '|'), '),') ";

Wow, I'm surprised no one ever mentioned this here.哇,我很惊讶这里从来没有人提到过这个。
In a nutshell, If you know the order of your members, then just query in a single bitwise operation.简而言之,如果您知道成员的顺序,那么只需在一次按位运算中进行查询。

SELECT * FROM example_table WHERE (example_set & mbits) = mbits;

Explanation:解释:

If we had a set that has members in this order: "HTML", "CSS", "PHP", "JS"... etc.如果我们有一个按以下顺序包含成员的集合:“HTML”、“CSS”、“PHP”、“JS”……等等。
That's how they're interpreted in MySQL:这就是它们在 MySQL 中的解释方式:

"HTML" = 0001 = 1
"CSS"  = 0010 = 2
"PHP"  = 0100 = 4
"JS"   = 1000 = 16

So for example, if you want to query all rows that have "HTML" and "CSS" in their sets, then you'll write因此,例如,如果您想查询其集合中包含“HTML”和“CSS”的所有行,那么您将编写

SELECT * FROM example_table WHERE (example_set & 3) = 3;

Because 0011 is 3 which is both 0001 "HTML" and 0010 "CSS" .因为00113 ,它既是0001 "HTML"又是0010 "CSS"

Your sets can still be queried using the other methods like REGEXP, LIKE, FIND_IN_SET(), and so on.仍然可以使用 REGEXP、LIKE、FIND_IN_SET() 等其他方法查询您的集合。 Use whatever you need.使用任何你需要的东西。

You can also use the like command for instance:例如,您还可以使用 like 命令:

where setcolumn like '%a,b%'

or或者

where 'a,b,c,d' like '%b,c%'

which might work in some situations.这在某些情况下可能有效。

您可以使用 in 从两个值中查找匹配值

SELECT * FROM table WHERE  myvals in (a,b,c,d)

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

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