DQL語言(二)條件查詢

2021-10-14 23:56:12 字數 3093 閱讀 8980

目錄

一、語法

二、篩選條件的分類

1、簡單條件運算子

> < = <> != >= <=  

<=>安全等於

2、按邏輯表示式篩選

3、模糊查詢

1. like

2. between and

3. in

4. is null

5. is null pk <=>

select 查詢列表 

from 表名 

where 篩選條件 

先執行from,再執行where,最後執行select

案例:

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

select

*from

employees

where

salary>12000;

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

select

last_name,

department_id

from

employees

where

department_id<>90;

安全等於既可以判斷null值,又可以判斷普通的數值

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

select

last_name,

commission_pct

from

employees

where

commission_pct <=>null;

#案例2:查詢工資為12000的員工資訊

select

last_name,

salary

from

employees

where

salary <=> 12000;

邏輯運算子:

作用:用於連線條件表示式

&& || !

and or not

&&和and:兩個條件都為true,結果為true,反之為false

||或or: 只要有乙個條件為true,結果為true,反之為false

!或not: 如果連線的條件本身為false,結果為true,反之為false

#案例1:查詢工資在10000到20000之間的員工名、工資以及獎金

select

last_name,

salary,

commission_pct

from

employees

where

salary>=10000 and salary<=20000;

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

select

*from

employees

where

not(department_id>=90 and department_id<=110) or salary>15000;

like

between and

inis null

特點:一般和萬用字元搭配使用

萬用字元:

% 任意多個字元,包含0個字元

_ 任意單個字元

#案例1:查詢員工名中包含字元a的員工資訊

select

*from

employees

where

last_name like '%a%';#abc

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

select

last_name,

salary

from

employees

where

last_name like '__e_a%';

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

select

last_name

from

employees

where

last_name like '_$_%' escape '$';

①使用between and 可以提高語句的簡潔度

②包含臨界值

③兩個臨界值不要調換順序

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

select

*from

employees

where

employee_id between 120 and 100;

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

特點:①使用in提高語句簡潔度

②in列表的值型別必須一致或相容

③in列表中不支援萬用字元

案例:查詢員工的工種編號是 it_prog、ad_vp、ad_pres中的乙個員工名和工種編號

select

last_name,

job_id

from

employees

where

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

=或<>不能用於判斷null值

is null或is not null 可以判斷null值

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

select

last_name,

commission_pct

from

employees

where

commission_pct is null;

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

select

last_name,

commission_pct

from

employees

where

commission_pct is not null;

is null:僅僅可以判斷null值,可讀性較高,建議使用

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

MySQL高階二 條件查詢

目錄 where 語法 執行順序 分類 1 按條件表示式篩選 2 按邏輯表示式篩選 3 模糊查詢 1 like 2 between and 3 in 4 is null 5 完全等於 該教程使用mysql5.5.27以及sqlyog安裝教程請參考mysql和sqlyog安裝教程 select 查詢列...

二 條件迴圈語句

1 查詢那些既可以被7整除又可以被5整除的數字,介於1500和2700之間 1 使用列表推導式 num i for i in range 1500 2700 if i 7 0and i 5 0 print num out 1505,1540,1575,1610,1645,1680,1715,1750...

python初學二 條件語句

1.句法 one way decision if two way decision if else multiway decision if elif else 2.try.except 語句 異常處理 如果try後的語句出現執行錯誤 程式會執行except後的語句 在執行try中的語句時在那條語句...