数据库应用2023-04-08

msql like _ vs %

In MySQL, the underscore (_) and percent sign (%) are wildcards used in LIKE expressions for pattern matching.

The underscore matches any single character, while the percent sign matches any sequence of zero or more characters. For example:

  • LIKE 'cat': matches any string that exactly equals 'cat'
  • LIKE 'c_t': matches any string that starts with 'c', ends with 't', and has one character between them (e.g., 'cat', 'cot', 'cut', etc.)
  • LIKE 'c%': matches any string that starts with 'c' (e.g., 'cat', 'can', 'cookie', etc.)
  • LIKE '%at': matches any string that ends with 'at' (e.g., 'cat', 'bat', 'flat', etc.)
  • LIKE '%a%': matches any string that contains the letter 'a' (e.g., 'cat', 'banana', 'apple', etc.)

Note that MySQL is case-insensitive by default, so LIKE 'CAT' is equivalent to LIKE 'cat'. However, you can change this behavior by setting the collation or using the BINARY keyword.

 

posted @ 2023-04-08 15:33  ChuckLu  阅读(11)  评论(0编辑  收藏  举报