第二節 查詢(DQL)

2021-10-14 22:48:35 字數 4424 閱讀 7867

①基礎查詢

#語法:

select 查詢列表 from 表名;

#特點:

1.查詢列表可以是:表中的字段、常量值、表示式、函式

2.查詢的結果是乙個虛擬的**

#查詢單個字段

select lastname from employees;

#查詢表中的多個字段

select last_name,salary,email from employees;

#查詢表中的所有字段

select from employees;

#查詢常量值

select 100;

select 'jhon';

#查詢表示式

select 10098;

#查詢函式

select version();

#起別名

1.便於理解

2.如果要查詢欄位有重名的情況,使用別名可區分開來

#方式一:使用as

select 100*98 as 結果;

select last_name as 姓,first_name as 名 from employees;

#方式二 :使用空格

select last_name 姓,first_name 名 from employees;

#案例:查詢salary,顯示結果為out put

select salary as 'out put' from employees;

#去重#案例:查詢員工表中涉及到的所有的部門編號

select distinct department_id from employees;

#+的作用:

1.運算子

select 100+90;兩個運算元都為數值型,則做加法運算

select '123'+90;其中一方為字元型,試圖將字元型數值轉換成數值型,

如果轉換成功,則繼續做加法運算,如果轉換失敗,

select 'jhon'+90;則將字元型數值轉換為0

select null+0;只要其中一方為null,則結果肯定為null

#concat實現連線

#案例:查詢員工名和姓連線成乙個字段,並顯示為姓名

select concat(last_name,first_name) as 姓名 from employees;

②條件查詢

#語法:

select 查詢列表 from 表名 where 篩選條件

#分類:

1.按條件表示式篩選

條件運算子:>、<、<=、!=、<>、>=、<=

2.按邏輯表示式篩選:

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

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

3.模糊查詢

like:

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

萬用字元:%任意多個字元

:任意多個字元

between and、 in、 is null、is not null

#條件運算子的使用

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

select from employees where salary > 12000;

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

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

#邏輯表示式篩選

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

select last_name,salary,commission_pct from employees where salary >= 10000 and salary <= 20000;

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

select from employees where not (departmentid>= 90 and department_id<= 110) or salary>15000;

#模糊查詢

1.like

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

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

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

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

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

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

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

2.between and

①可以提高語句的簡潔度

②包含臨界值

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

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

select * from employees where employee_id between 100 and 120;

3.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');

4.is null

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

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

select last_name,commission_pct from employees where commission_pct is null;

select last_name,commission_pct from employees where commission_pct is not null;

#安全等於:<=>

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

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

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

#查詢工資為12000的員工資訊

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

③排序查詢

#語法

select * from 表 【where 篩選條件】 order by 排序列表 【asc|desc】

①asc:公升序\desc:降序,如果不寫預設公升序

②order by子句支援單個字段、多個字段、表示式、函式、別名

③order by子句放在查詢語句的最後面,limit子句除外

#案例一:查詢員工資訊,要求工資從高到低排序

select * from employees order by salary desc;

select * from employees order by salary asc;

#案例二:查詢部門編號》=90的員工資訊,按入職的時間的先後進行排序【新增篩選條件】

select * from employees where department_id >= 90 order by hiredate asc;

#案例三:按年薪的高低顯示員工的資訊和年薪【按表示式排序】

select *,salary*12*(1+ifnull(commission_pct,0)) 年薪 from employees order by salary*12*(1+ifnull(commission_pct,0))desc;

#案例四:按年薪的高低顯示員工的資訊和年薪【按別名排序】

select *,salary*12*(1+ifnull(commission_pct,0)) 年薪 from employees order by 年薪 desc;

#案例五:按姓名的長度顯示員工的姓名和工資【按函式排序】

select length(last_name) 位元組長度,last_name,salary from employees order by length(last_name) desc;

#案例六:查詢員工資訊,要求先按工資排序,再按員工編號排序【按多個字段】

select * from employees order by salary asc,employee_id desc;

彙編 第二節

第二節 cpu主要有有運算器,控制器,暫存器,三部分組成 8086 cpu有14個暫存器,如ax,bx,cx,dx,si。8086所有暫存器都是16位的,可以存放2個byte即位元組,16位二進位制數 所能存放的最大數值是 11111111111 2 16 1 從0開始,所以 1嘛 通用暫存器為4個...

函式第二節

coding utf 8 1.定義乙個方法get num num num引數是列表型別,判斷列表裡面的元素為數字型別。其他型別則報錯,並且返回乙個偶數列表 注 列表裡面的元素為偶數 def get num l l for i in l 判斷列表l的元素是否都為整形 if not isinstance...

第二節練習

a b 2 鏈式賦值 print a print b c id a 位址print c c type a 型別 print c a,b 2,3 系列解包賦值 a,b b,a print a print b 基本運算 print 3 2 print 30 5 print 30 5 print 30 5...