­

資料庫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)