日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

python count函数用法 comm_python3:MySQL 8.0学习笔记(第五部分:单表查询操作)

發布時間:2024/7/23 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python count函数用法 comm_python3:MySQL 8.0学习笔记(第五部分:单表查询操作) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在講解單表查詢時,首先創建一個emp的員工表,表中字段包括:empno(員工編號)、ename(員工姓名)、job(員工職位)、mgr(員工領導)、hiredate(員工入職日期)、sal(員工月薪)、comm(員工津貼)、deptno(員工部門編號):

create table emp(

emp_id int(4) primary key,

emp_name varchar(5),

emp_job varchar(10),

emp_mgr varchar(5),

emp_hiredate date,

emp_sal decimal(7,2),

emp_comm decimal(7,2),

emp_deptno int(2)

);

為emp員工表插入多條數據:

insert emp values

(7369, 'Smith', 'clerk', 7902, '1980-12-17', 800, null, 20),

(7499, 'Allen', 'salesman', 7698, '1981-02-20', 1600, 300, 30),

(7521, 'Ward', 'salesman', 7698, '1981-02-22', 1250, 500, 30),

(7566, 'Jones', 'manager', 7839, '1981-04-02', 2975, null, 20),

(7654, 'Maritn', 'salesman', 7698, '1981-09-28', 1250, 1400, 30),

(7698, 'Blake', 'manager', 7839, '1981-05-01', 2850, null, 30),

(7782, 'Clark', 'manager', 7839, '1981-06-09', 2450, null, 10),

(7788, 'Scott', 'analyst', 7566, '1987-04-19', 3000, null, 20),

(7839, 'King', 'president', null, '1981-11-17', 5000, null, 10),

(7844, 'Turner', 'salesman', 7698, '1981-09-08', 1500, 0, 30),

(7876, 'Adams', 'clerk', 7788, '1987-05-23', 1100, null, 20),

(7900, 'James', 'clerk', 7698, '1981-12-03', 950, null, 30),

(7902, 'Ford', 'analyst', 7566, '1981-12-03', 3000, null, 20),

(7934, 'Miller', 'clerk', 7782, '1982-01-23', 1300, null, 10);

1.簡單查詢:

1.查詢所有數據:

select * from emp;

2.查詢指定數據

select emp_id,emp_name,emp_hiredate from emp;

3.去重復記錄的查詢:

select distinct emp_deptno from emp;

4.使用算術運算符的查詢:

select emp_id,emp_name,emp_job,emp_sal*12 from emp;

5.使用字段別名的查詢:

select emp_id,emp_name,emp_job as work,emp_sal*12 as yearsalary from emp;

6.設置數據顯示格式的查詢:

select concat('工號:',emp_id,',姓名:',emp_name,',部門:',emp_deptno,',職位:',emp_job,',年薪:',emp_sal*12) as info from emp;#如果數據中有一個為null,則返回null

2.對查詢結果排序:

1.按照指定的單字段排序:

select emp_id,emp_name,emp_job,emp_sal*12 as year_sal from emp order by year_sal;

2.按照指定的多字段排序(asc和desc分別表示升序和降序,升序時可不寫asc):

select emp_id,emp_name,emp_job,emp_sal*12 as year_sal from emp order by year_sal,emp_name;

3.條件查詢:

1.使用比較運算符進行查詢:

select emp_id,emp_name,emp_job from emp where emp_job='clerk';

2.使用[not] between and 的范圍查詢:

select emp_id,emp_name,emp_job from emp where emp_id between 7800 and 8000;

3.使用in的條件查詢:

select emp_id,emp_name,emp_job from emp where emp_job in ('clerk');

4.使用is [not] null的空值查詢:

select emp_id,emp_name,emp_job,emp_comm from emp where emp_comm is null;

5.使用[not] like的模糊查詢:

a.使用 “%”通配符的模糊查詢:

select * from emp where emp_name like 'S%';

b.使用“_”通配符的模糊查詢:

select * from emp where emp_name like "_l%";

c.使用not like的模糊查詢:

select * from emp where emp_name not like "%s%";

6.使用and的多條件查詢:

select * from emp where emp_name like "%s%" and emp_sal>1200;

7.使用or的多條件查詢:

select * from emp where emp_name not like "%s%" or emp_sal >2000;

4.限制查詢:

1.不指定初始位置的限制查詢:

select * from emp where emp_name not like "%S%" limit 3;

2.指定初始位置的限制查詢:

select * from emp where emp_name not like "%S%" limit 3,3;

5.使用函數查詢:

1.單行函數:

1->:select concat('工號:',emp_id,',姓名:',emp_name,',工資:',emp_sal) as info from emp where emp_sal>1000;#concat函數

2->:select * from emp where length(emp_name)> 5;#length函數

3->:select * ,lower(emp_name),upper(emp_name) from emp where emp_sal>2000;#lower,upper函數

4->:select * ,replace(emp_job,'clerk','staff') newjob from emp where emp_job = 'clerk';#replace函數

5->:select *,substring(emp_name,1,3) subname from emp where emp_job = 'clerk';#substring函數

2.數值函數:

MySQL為我們提供了一個名為'dual'的虛擬表,我們可以用它來進行演示,如果沒有where語句,我們可以省略from dual語句。

1.abs(),ceil(),floor(),mod(),pi(),pow()函數的使用:

1->:select abs(-1.2);#取絕對值

2->:select ceil(1.2);#向上取值

3->:select floor(1.2);#向下取值

4->:select mod(5,2);#取模

5->:select pi();#取圓周率的值

6->:select pow(2,3);取指數的值,2的3次方

2.rand()函數的使用(產生0,1之間的隨機數值)

1->:select rand(),rand();#產生兩個0,1之間的隨機數

2->:select rand()*5;#產生0,5之間的隨機數

3->:select floor(rand()*5);#產生0,5之間的隨機整數,5產生的概率基本為0

4->:select round(pi()),round(pi(),2),round(pi(),4);#取小數點后位數,四舍五入

5->:select truncate(pi(),0),truncate(pi(),2),truncate(pi(),4);#取小數點后位數,直接截斷,而不是四舍五入,且位數無默認位。

2.日期與時間函數:

1.使用curdata(),curtime(),now()函數查詢:

select curdate(),curtime(),now();#獲取日期,時間與日期時間

2.systdate()函數的使用:

select sysdate();#獲得系統時間

3.使用dayofyear(),dayofweek(),week()函數查詢:

select dayofyear(curdate()),dayofweek(curdate());#查詢當前日期為該年第多少天,該周多少周(從周日起算)

select week(curdate());#查詢當前日期位于該年多少周

4.使用date_add(),date_sub()函數查詢

select date_add(curdate(),interval '10' day);#interval(間隔) day為間隔類型

select date_sub(curdate(),interval '10' day);

5.使用datediff()函數查詢

select datediff('2019-5-20',curdate());#計算日期間隔

4.流程函數

1.使用if()函數查詢:

select emp_id,emp_name,emp_sal,if(emp_sal>2000,'high','low') as sal_level from emp where emp_deptno = 20;

2.使用ifnull()函數查詢:

1->:select emp_id,emp_name,emp_sal,emp_comm,emp_sal*12+emp_comm as year_income from emp where emp_deptno = 30;#不使用ifnull()函數,與使用進行對比

2->:select emp_id,emp_name,emp_sal,emp_comm,emp_sal*12+ifnull(emp_comm,0) as year_income from emp where emp_deptno = 30;#使用ifnull()函數

3.使用nullif()函數查詢:

select nullif(1,1),nullif(1,2);

4.使用case...when...then...else...end...()函數查詢:

select emp_name,emp_sal,emp_deptno,case emp_sal>=3000 when true then 'high' else 'low' end sal_level from emp where deptno = 20;

2.多行函數:

1.count()函數的使用:

select count(*) from emp;

select count(emp_comm),count(emp_id) from emp;

2.sum()和avg()函數的使用:

select sum(emp_sal) sum_sal,avg(emp_sal) avg_sal from emp;#求和、計算平均值

3.max()和min()函數的使用:

select max(emp_sal),min(emp_sal) from emp;

6.分組查詢:

1.簡單的group by 查詢:

select * from emp group by emp_deptno;

select * from emp group by emp_job,emp_deptno;

2.group by與統計函數:

select emp_deptno,count(*),max(emp_sal),min(emp_sal),sum(emp_sal),avg(emp_sal) from emp group by emp_deptno;

3.group by與group_concat()的分組查詢:

select emp_deptno,count(*),group_concat(emp_name) from emp group by deptno;

4.group by與having:

select emp_deptno,count(*),max(emp_sal),min(emp_sal),sum(emp_sal),avg(emp_sal) from emp group by emp_deptno having avg(emp_sal)>=2000;

MySQL中各子句的執行過程由先到后依次為:from->where->group by->having->select->order by。

總結

以上是生活随笔為你收集整理的python count函数用法 comm_python3:MySQL 8.0学习笔记(第五部分:单表查询操作)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。