1. 程式人生 > 其它 >mysql之模糊查詢3

mysql之模糊查詢3

模糊查詢

1.like

一般和萬用字元搭配使用

  •   % 任意多個字元(包含0個字元)  
  •   _任意單個字元

注意:要查含—的資訊可以用 \ 轉義 ‘\_’

   或者用ESCAPE (select * from employees where last_name like '_A_%' ESCAPE 'A';)

例1:查詢員工表包含字元a的員工資訊

select * from employees where last_name like '%a%';

例2:查詢員工表包含第三個字元是a的員工資訊

select * from employees where last_name like

'__a%';

2.between and

  • 值不能顛倒
  • 更簡潔
  • 包含臨界值

例1:查詢員工編號在100到200之間的員工資訊

select * from employees whereemploye_id >= 100 and employe_id <= 120;

select * from employees where between 100 and 120;(更簡潔)

3.in

  • 判斷某欄位的值是屬於in列表的某一項
  • 使用提高語句的簡潔度
  • in列表的值型別必須一致
  • in裡面不支援使用萬用字元

例1:查詢員工工種編號是 IT_PROG,AD_VP,AD-PRES 中的一個員工名和工種編號

select last_name,job_id from employees where job_id in

('IT_PROG','AD_VP','AD-PRES');

4.is null 和 is not null

例1:例1:查詢沒有(有)獎金員工姓名和獎金率

select last_name,commision_pct from employees where commision_pct is (not) null;

5.安全對於<=>

select last_name,commision_pct from employees where commision_pct <=> null;

is null 和 <=> 比較

is null :僅僅可以判斷null值但是可讀性高

<=>:既可以判斷null值又可以判斷普通數值,可讀性低