2

Using the LIKE operator in MySQL is to tell MySQL that the following search patterns are matched using wildcards.

percent wildcard

The percent sign represents any number of occurrences of any character. For example, the following sql can find all p_name products starting with apple in produces:

 SELECT p_id FROM products WHERE p_name LIKE 'apple%'
  • Note that trailing spaces will lead to unsuccessful matching. Solution 1. Add % at the end; Solution 2. Use a function to remove trailing spaces (recommended).

underscore wildcard

The underscore wildcard can represent any character appearing once

 SELECT p_id FROM products WHERE p_name LIKE '_o_'

📢 Wildcard search is inefficient and should be used with caution.


Reference: Forta B. MySQL crash course[M]. Pearson Education India, 2006.

LiberHome
409 声望1.1k 粉丝

有问题 欢迎发邮件 📩 liberhome@163.com