02 MySQL DQL 條件查詢

2022-08-19 02:33:07 字數 3126 閱讀 1839

#查詢2:條件查詢

分類:1)按條件表示式篩選

條件運算子:> < = !=(<>也可以) >= <=

2)按邏輯表示式篩選

邏輯運算子:and or not(&& || !)

作用:連線條件表示式

&& and 兩個條件都為true

|| or 兩個條件任一為true

! not 取反

3) 模糊查詢

like

between and

inis null | is not null

*/use myemployees;

#一、按條件表示式篩選

#案例1:查詢工資大於12000的員工資訊

select * from employees where salary > 12000;

#案例2:查詢部門編號不等於90的員工號和部門編號

select employee_id,department_id from employees where department_id != 90;

select employee_id,department_id from employees where department_id <>90;

#二、按邏輯表示式篩選

#案例1:查詢工資在10000到20000之間的員工資訊

select last_name,salary,ifnull(commission_pct,0) as commission

from employees where salary>=10000 and salary<=20000;

#案例2:查詢部門編號不在90~110之間,或者工資高於15000的員工資訊

select last_name,department_id,salary from employees

where department_id<90 or department_id>110 or salary>15000;

#三、模糊查詢

/*like

一般和萬用字元配合使用

% 任意多個字元(0,1,無限多個)

_ 任意單個字元(1個)

between and

inis null | is not null

*/#like模糊查詢

注意:mysql5.5及以上的版本,也支援數值型的like查詢

#案例1:查詢員工名中包含字元a的員工資訊(mysql沒有字串的概念,都是字元)

# % 表示任意個數的任意字元

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

#案例2:查詢員工名中第三個字元為n, 第五個字元為l的員工名和工資

select last_name,salary from employees where last_name like '__n_l%';

#案例3:查詢員工名中第二個字元為_的員工名

#萬用字元轉移為普通字元,\%, \_

select last_name from employees where last_name like '_\_%';

#指定任意乙個字元為轉義字元

#escape '$' 指定$為轉義字元

select last_name from employees where last_name like '_$_%' escape '$';

#案例:數值型的like查詢 (數值->字元->然後判斷)

select * from employees where department_id like '1__';

#2、between and模糊查詢

/*提高語句簡潔讀

包含臨界值

臨界值不能顛倒順序(等價於 >=左邊,<=右邊)

臨界值要型別一致,或者能隱式轉換

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

#between 100 and 200, 100和200包括在內

select * from employees where employee_id between 100 and 200;

#3、in 模糊查詢

/*含義:判斷某字段的值,是否屬於in列表中的某一項

in (v1,v2,v3,...)

特點:1)比使用or,語句簡潔度高

2)in列表的值型別必須統一,或者可以隱式轉換

3)in列表的值,不支援萬用字元(like才能解析萬用字元)

*/#案例1:查詢工種是 it_prog, ad_vp, ad_pres 其中任意乙個的員工名和工種id

select last_name,job_id from employees

where

job_id='it_prot' or job_id='ad_vp' or job_id='ad_pres';

select last_name,job_id from employees

where

job_id in ('it_prot','ad_vp','ad_pres');

#4、is null

/*= 不能判斷null值

is null 判斷是否為null

is not null 判斷是否為非null

*/#案例1:查詢沒有獎金的員工名和獎金率(替換為0)

select last_name, ifnull(commission_pct,0) as commission

from employees

where commission_pct is null;

#案例2:查詢有獎金的員工名和獎金率

select last_name, ifnull(commission_pct,0) as commission

from employees

where commission_pct is not null;

#5、安全等於 <=>

/*<=> null, 可以判斷是否為null值

<=> 普通值

*/select last_name,commission_pct from employees where commission_pct <=> null;

select last_name,salary from employees where salary <=> 12000;

MySQL DQL 多表查詢

create table stu id int primary key auto increment name varchar 20 gender varchar 20 math double insert into stu values null,zhangsan male 89.5 null,l...

MySQL DQL 查詢資料

1.查詢所有資料select from 表名 2.條件查詢select from 表名 where 條件 3.查詢部分字段select 欄位1 欄位2.from 表名 where 條件 別名查詢 as 列別名,就是給字段起乙個別名,方便後面操作和檢視 別名as 可以省略 引號可以不加 查詢user表...

MySQL DQL聯合查詢

union 聯合 合併 將多條查詢語句的結果合併成乙個結果 查詢語句1 union 查詢語句2 union.要求多條查詢語句的查詢列數是一致的 要求多條查詢語句的每一列的型別和順序最好一致 union自動會去重,如果使用union all可以包含重複項 案例 查詢部門編號 90或郵箱包含a的員工資訊...