SQL Like and like

前端 未结 5 1347
孤独总比滥情好
孤独总比滥情好 2020-12-11 23:07

Is it possible to string together multiple SQL LIKE wildcards in one query - something like this?

LIKE \'% aaaa %\' AND LIKE \'% bbbb %\'

T

相关标签:
5条回答
  • 2020-12-11 23:59

    The correct SQL syntax is:

    field LIKE '% aaaa %' AND field LIKE '% bbbb %'
    
    0 讨论(0)
  • 2020-12-12 00:00

    Yes, that will work, but the syntax is:

    Field LIKE '%aaa%' AND field LIKE '%bbb%'
    
    0 讨论(0)
  • 2020-12-12 00:02

    This is useful when you are using this statement with variables.

    SELECT * 
    FROM `tableName`
    WHERE `colName` LIKE CONCAT('%', 'aaaa', '%') AND -- if aaaa is direct Text
          `colName` LIKE CONCAT('%', 'bbbb', '%')
    
    SELECT * 
    FROM `tableName`
    WHERE `colName` LIKE CONCAT('%', aaaa, '%') AND -- if aaaa is variable
          `colName` LIKE CONCAT('%', bbbb, '%')
    
    0 讨论(0)
  • 2020-12-12 00:05

    It is possible to string together an arbitrary number of conditions. However, it's prudent to use parenthesis to group the clauses to remove any ambiguity:

    SELECT *
      FROM tableName
     WHERE (columnOne LIKE '%pattern%')
       AND (columnTwo LIKE '%other%')
    
    0 讨论(0)
  • 2020-12-12 00:06

    Yes, but remember that LIKE is an operator similar to == or > in other languages. You still have to specify the other side of the equation:

    SELECT * FROM myTable
    WHERE myField LIKE '%aaaa%' AND myField LIKE '%bbbb%'
    
    0 讨论(0)
提交回复
热议问题