MySQL IN with LIKE

前端 未结 3 2135
忘了有多久
忘了有多久 2020-12-11 09:46

How would I use a IN table with like? So that I could use % in them? By in I mean:

SELECT fields 
  FROM table 
 WHERE age = \"50\" 
   AND name IN (\"tim\         


        
3条回答
  •  执念已碎
    2020-12-11 10:46

    There is no combination of the LIKE and IN clauses. It's either one, or the other, syntax:

    SELECT fields
      FROM table
     WHERE age = 50
       AND (   name IN ('tim', 'bob', 'nancy', 'john')
            OR name LIKE '2010-09-17%'
            OR name LIKE '2010-09-16%')
    

    The alternative to consider when searching text is Full Text Search (FTS):

    SELECT fields
      FROM table
     WHERE age = 50
       AND MATCH(name) AGAINST('tim bob nancy john')
    

    ...but this requires MyISAM tables, and Full Text Indexing.

提交回复
热议问题