[NOT] LIKE '字符串'
其中:a%b
表示以字母 a 开头,以字母 b 结尾的任意长度的字符串。该字符串可以代表 ab、acb、accb、accrb 等字符串。mysql> SELECT name FROM tb_students_info -> WHERE name LIKE 'T%'; +--------+ | name | +--------+ | Thomas | | Tom | +--------+ 2 rows in set (0.12 sec)可以看到,查询结果中只返回了以字母“T”开头的学生姓名。
mysql> SELECT NAME FROM tb_students_info -> WHERE NAME NOT LIKE 'T%'; +-------+ | NAME | +-------+ | Dany | | Green | | Henry | | Jane | | Jim | | John | | Lily | | Susan | +-------+ 8 rows in set (0.00 sec)可以看到,查询结果中返回了不以字母“T”开头的学生姓名。
mysql> SELECT name FROM tb_students_info -> WHERE name LIKE '%e%'; +-------+ | name | +-------+ | Green | | Henry | | Jane | +-------+ 3 rows in set (0.00 sec)可以看到,查询结果中返回了所有包含字母“e”的学生姓名。
a_b
可以代表 acb、adb、aub 等字符串。mysql> SELECT name FROM tb_students_info -> WHERE name LIKE '____y'; +-------+ | name | +-------+ | Henry | +-------+ 1 row in set (0.00 sec)
mysql> SELECT name FROM tb_students_info WHERE name LIKE 't%'; +--------+ | name | +--------+ | Thomas | | Tom | +--------+ 2 rows in set (0.00 sec) mysql> SELECT name FROM tb_students_info WHERE name LIKE BINARY 't%'; Empty set (0.01 sec)由结果可以看到,区分大小写后,“Tom”和“Thomas”等记录就不会被匹配到了。
mysql> SELECT NAME FROM test.`tb_students_info` WHERE NAME LIKE '%\%'; +-------+ | NAME | +-------+ | Dany% | +-------+ 1 row in set (0.00 sec)
本文链接:http://task.lmcjl.com/news/18909.html