MySQL資料庫 資料查詢

2021-10-05 16:23:51 字數 2833 閱讀 4642

#高階2:條件查詢

/*語法:

select

查詢列表

from

表名where

篩選條件;

分類:一、按條件表示式篩選

條件運算子:

> < != <> >= <=

二、按邏輯運算子篩選

邏輯運算子:

&& || !

and or not

三、模糊查詢

like

between

inis null

*/#一、按條件表示式篩選

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

select

*from

employees

where

salary>12000;

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

select

last_name,

department_id

from

employees

where

department_id<>90;

#二、按邏輯表示式篩選

#案例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

department_id<90 or department_id>110 or salary>150000;

#三:模糊查詢

/*like

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

萬用字元:

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

_ 任意乙個字元

between and

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

②包含臨界值

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

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

特點: ①使用in可以提高於都簡潔度

②in列表的值必須一直或相容

③在in裡面不支援萬用字元

is null|is not null

=或<>不能用來判讀null值

is null或is not null可以判讀null值 */

#1.like

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

select

*from

employees

where

last_name like '%a%';

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

select

*from

employees

where

last_name like '__n_l%';

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

select

*from

employees

where

last_name like '_\_%';# "/"是轉義字元

#2.between and

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

select

*from

employees

where

employee_id between 100 and 120;

#3.in

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

select

last_name,

job_id

from

employees

where

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

#等價於 job_id = 'it_prot' or job_id = 'ad_vp' or job_id = 'ad_pres';

#4.is null

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

select

last_name,

commission_pct

from

employees

where

commission_pct is null;#加上not就是查詢沒有獎金的

#<=>安全等於

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

select

last_name,

commission_pct

from

employees

where

commission_pct <=> null;#加上not就是查詢沒有獎金的

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

select

last_name,

salary

from

employees

where

salary <=> 12000;

#is null pk <=>

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

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

資料庫 資料查詢(三)

1 表示任意的一位字元 2 表示任意位數的任意字元 3 要實現模糊查詢需要使用到關鍵字 like 基本語法 select from 資料 where 模糊查詢字段 like 模糊查詢的關鍵字 注 模糊查詢要在 where 子句中去使用 例 查詢emp表中姓名是以a開頭的雇員資訊 select fro...

提公升資料庫資料查詢效率

1.前言 隨著資訊科技的發展,資訊系統在企業中的應用也越來越廣泛,資訊系統在企業運營中扮演者十分重要的角色。可以說在資訊化如此廣泛的今天,企業資訊化是提公升企業運營效率的必經之路,資料是企業資產中必不可少的組成部分,資料組織效率的高低可能直接影響企業業務的進展,資料的安全則關係到整個企業的命運與興亡...

php資料庫中資料查詢

mysql fetch row,mysql fetch array,mysql fetch object,mysql fetch assoc 區別用法 所以mysql fetch array 函式在某種程度上可以算是mysql fetch row 與 mysql fetch assoc 的集合 my...