MySQL LIKE IN()?

后端 未结 11 1675
时光说笑
时光说笑 2020-11-22 05:31

My current query looks like this:

SELECT * FROM fiberbox f WHERE f.fiberBox LIKE \'%1740 %\' OR f.fiberBox LIKE \'%1938 %\' OR f.fiberBox LIKE \'%1940 %\'


        
相关标签:
11条回答
  • 2020-11-22 05:44

    This would be correct:

    SELECT * FROM table WHERE field regexp concat_ws("|",(
    "111",
    "222",
    "333"
    ));
    
    0 讨论(0)
  • 2020-11-22 05:50

    You can create an inline view or a temporary table, fill it with you values and issue this:

    SELECT  *
    FROM    fiberbox f
    JOIN    (
            SELECT '%1740%' AS cond
            UNION ALL
            SELECT '%1938%' AS cond
            UNION ALL
            SELECT '%1940%' AS cond
            ) с
    ON      f.fiberBox LIKE cond
    

    This, however, can return you multiple rows for a fiberbox that is something like '1740, 1938', so this query can fit you better:

    SELECT  *
    FROM    fiberbox f
    WHERE   EXISTS
            (
            SELECT  1
            FROM    (
                    SELECT '%1740%' AS cond
                    UNION ALL
                    SELECT '%1938%' AS cond
                    UNION ALL
                    SELECT '%1940%' AS cond
                    ) с
            WHERE   f.fiberbox LIKE cond
            )
    
    0 讨论(0)
  • 2020-11-22 05:50

    You can get desired result with help of Regular Expressions.

    SELECT fiberbox from fiberbox where fiberbox REGEXP '[1740|1938|1940]';
    

    We can test the above query please click SQL fiddle

    SELECT fiberbox from fiberbox where fiberbox REGEXP '[174019381940]';
    

    We can test the above query please click SQL fiddle

    0 讨论(0)
  • 2020-11-22 06:00

    Sorry, there is no operation similar to LIKE IN in mysql.

    If you want to use the LIKE operator without a join, you'll have to do it this way:

    (field LIKE value OR field LIKE value OR field LIKE value)
    

    You know, MySQL will not optimize that query, FYI.

    0 讨论(0)
  • 2020-11-22 06:00

    You can use like this too:

    SELECT * FROM fiberbox WHERE fiber IN('140 ', '1938 ', '1940 ')
    
    0 讨论(0)
  • 2020-11-22 06:01

    A REGEXP might be more efficient, but you'd have to benchmark it to be sure, e.g.

    SELECT * from fiberbox where field REGEXP '1740|1938|1940'; 
    
    0 讨论(0)
提交回复
热议问题