Is there a combination of “LIKE” and “IN” in SQL?

后端 未结 25 2205
灰色年华
灰色年华 2020-11-22 03:08

In SQL I (sadly) often have to use \"LIKE\" conditions due to databases that violate nearly every rule of normalization. I can\'t change that right now. But tha

25条回答
  •  猫巷女王i
    2020-11-22 03:56

    Another solution, should work on any RDBMS:

    WHERE EXISTS (SELECT 1
                    FROM (SELECT 'bla%' pattern FROM dual UNION ALL
                          SELECT '%foo%'        FROM dual UNION ALL
                          SELECT 'batz%'        FROM dual)
                   WHERE something LIKE pattern)
    

    The inner select can be replaced by another source of patterns like a table (or a view) in this way:

    WHERE EXISTS (SELECT 1
                    FROM table_of_patterns t
                   WHERE something LIKE t.pattern)
    

    table_of_patterns should contain at least a column pattern, and can be populated like this:

    INSERT INTO table_of_patterns(pattern) VALUES ('bla%');
    INSERT INTO table_of_patterns(pattern) VALUES ('%foo%');
    INSERT INTO table_of_patterns(pattern) VALUES ('batz%');
    

提交回复
热议问题