数据库MySQL-模糊查询

  • 2020 年 3 月 27 日
  • 筆記

1.7 模糊查询

1.7.1 通配符

  1. _ [下划线] 表示任意一个字符
  2. % 表示任意字符

练习

1、满足“T_m”的有(A、C)  A:Tom         B:Toom       C:Tam         D:Tm     E:Tmo    2、满足“T_m_”的有( B C)  A:Tmom   B:Tmmm  C:T1m2    D:Tmm     E:Tm    3、满足“张%”的是(ABCD)  A:张三     B:张三丰     C:张牙舞爪      D:张      E:小张    4、满足“%诺基亚%”的是(ABCD)  A:诺基亚2100   B:2100诺基亚   C:把我的诺基亚拿过来   D:诺基亚

1.7.2 模糊查询(like)

模糊查询的条件不能用’=’,要使用like。

mysql> select * from stu where stuname like 'T_m';  +--------+---------+--------+--------+---------+------------+------+------+  | stuNo  | stuName | stuSex | stuAge | stuSeat | stuAddress | ch   | math |  +--------+---------+--------+--------+---------+------------+------+------+  | s25320 | Tom     | 男       |     24 |       8 | 北京           |   65 |   67 |  +--------+---------+--------+--------+---------+------------+------+------+  1 row in set (0.00 sec)    -- 查询姓张的学生  mysql> select * from stu where stuname like '张%';  +--------+---------+--------+--------+---------+------------+------+------+  | stuNo  | stuName | stuSex | stuAge | stuSeat | stuAddress | ch   | math |  +--------+---------+--------+--------+---------+------------+------+------+  | s25301 | 张秋丽   | 男     |     18 |       1 | 北京        |   80 | NULL |  +--------+---------+--------+--------+---------+------------+------+------+  1 row in set (0.00 sec)