欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python全棧之學習MySQL(2)

 更新時間:2022年01月24日 17:21:54   作者:熬夜泡枸杞  
這篇文章主要為大家介紹了Python全棧之MySQL,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

1. mysql_where子句_聚合函數(shù)

# ### part 單表查詢
""" select ... from ... where ... group by ... having ... order by ... limit ...  """
# 一.where 條件的使用
	"""
		功能: 對表中的數(shù)據(jù)進行帥選和過濾
		語法:
			1.判斷的符號
			= (!= <>不等于) > >= < <=
			2.拼接不同的條件的關(guān)鍵字
			and or not 
			3.查詢對應的區(qū)間值
			between 小值 and 大值 [小值,大值]   查詢兩者之間的范圍值
			4.查詢具體在哪個范圍中
			in(1,21,333,444) 指定范圍
			5.模糊查詢 like % 通配符  _ 通配符
				like "%b"  匹配以b結(jié)尾的任意長度的字符串
				like "b%"  匹配以b開頭的任意長度的字符串
				like "%b%" 匹配字符串中含有b的任意長度的內(nèi)容
				like "__b" 匹配總長度為3個字符,任意內(nèi)容的字符串,并且以b結(jié)尾
				like "b_"  匹配總長度為2個字符,任意內(nèi)容的字符串,并且以b開頭
	"""
	# 1. 查詢部門是sale的所有員工姓名:
	select emp_name from employee where post="sale";
	# 2. 部門是teacher , 收入大于10000的所有數(shù)據(jù)
	select * from employee where post = "teacher" and salary > 10000;
	# 3. 收入在1萬到2萬之間的所有員工姓名和收入
	select emp_name,salary from employee where salary between 10000 and 20000;
	# 4. 收入不在1萬到2萬之間的所有員工姓名和收入
	select emp_name,salary from employee where salary not between 10000 and 20000;
	# 5. 查看崗位描述為NULL的員工信息
	select emp_name from employee where post_comment = null;
	select emp_name from employee where post_comment = '';
	select emp_name from employee where post_comment is null;
	# 6. 查看崗位描述不為NULL的員工信息
	select emp_name from employee where post_comment is not null;
	# 7. 查詢收入是3000 ,4000 ,5000,8300 所有員工的姓名和收入
	select emp_name,salary from employee where salary in(3000,4000,5000,8300);
	select emp_name,salary from employee where salary = 3000 or salary=4000 or salary=5000 or salary=8300;
	# 8. 查詢收入不是3000 ,4000 ,5000,8300 所有員工的姓名和收入
	select emp_name,salary from employee where salary not in(3000,4000,5000,8300);
	# 9. 以on結(jié)尾的員工名搜一下
	select emp_name from employee where emp_name like "%on";
	select emp_name from employee where emp_name like "ji%";
	select emp_name from employee where emp_name like "_le_";
	# 10. 統(tǒng)計員工一年的年薪
	select concat(" 姓名: ",emp_name,"  收入:  ",salary) from employee;
	# 計算年薪,可以在mysql中使用四則運算符 + - * / 
	select concat(" 姓名: ",emp_name,"  收入:  ",salary * 12) from employee;
	select concat_ws("  :  ",emp_name,salary*12 ) from employee;
	# 11. 查詢部門的種類
	# distinct  返回唯一不同的值
	select distinct(post)  from employee;
	
# 二.group by 子句 分組分類
	"""group by 字段,對數(shù)據(jù)進行分類, by后面接什么字段,select后面就搜什么字段"""
	select sex from  employee group by sex;
	# group_concat 按照分組把對應字段拼在一起;
	select group_concat(emp_name),post from  employee group by post;
	# 聚合函數(shù)
		# count 統(tǒng)計總數(shù) *所有
		select count(*) from employee;
		# max  統(tǒng)計最大值
		select max(salary) from employee;
		# min  統(tǒng)計最小值
		select min(salary) from employee;
		# avg  統(tǒng)計平均值
		select avg(salary) from employee;
		# sum  統(tǒng)計總和
		select sum(salary) from employee;
	# 1. 查詢部門名以及各部門的平均薪資
	select avg(salary),post from employee group by post;
	# 2. 查詢部門名以及各部門的最高薪資
	select max(salary),post from employee group by post;
	# 3. 查詢部門名以及各部門的最低薪資
	select min(salary),post from employee group by post;
	# 4. 查詢公司內(nèi)男員工和女員工的個數(shù)
	select count(*),sex from employee group by sex;
	# 5. 查詢部門名以及部門包含的所有員工名字
	select group_concat(emp_name),post from employee group by post;
	# 6 可以group by 兩個字段,就可以同時搜索兩個字段
	select emp_name,post from employee group by post ,emp_name;

2. mysql_其他子句語法

# 三.having 在數(shù)據(jù)分類分組之后,對數(shù)據(jù)進行二次過濾,一般配合group by來使用的;
	# 找出各部門平均薪資,并且大于10000
	select post , avg(salary) from  employee group by post having avg(salary) > 10000
	# 1.查詢各崗位內(nèi)包含的員工個數(shù)小于2的崗位名、崗位內(nèi)包含員工名字、個數(shù)
	select post , group_concat(emp_name), count(*) from employee group by post having count(*) < 2;
	# 2.查詢各崗位平均薪資小于10000的崗位名、平均工資
	select post , avg(salary) from employee group by post having avg(salary) < 10000
	# 3.查詢各崗位平均薪資大于10000且小于20000的崗位名、平均工資
	select post, avg(salary) from employee group by post having avg(salary) between 10000 and 20000
	select post, avg(salary) from employee group by post having avg(salary) > 10000 and  avg(salary) < 20000;
	
# 四.order by 排序 , 按照某字段排序
	order by age asc (升序) order by age desc (降序)
	# 按照年齡從小到大排序
	select * from employee order by age;
	# 按照年齡從大到小排序
	select * from employee order by age desc;
	# 1. 查詢所有員工信息,先按照age升序排序,如果age相同則按照hire_date降序排序
	select * from employee order by age asc ,  hire_date desc;
	# 2. 查詢各崗位平均薪資大于10000的崗位名、平均工資,結(jié)果按平均薪資升序排列
	select post,avg(salary) from employee group by post having avg(salary) > 10000 order by avg(salary) asc
	# 3. 查詢各崗位平均薪資大于10000的崗位名、平均工資,結(jié)果按平均薪資降序排列
	select post,avg(salary) from employee group by post having avg(salary) > 10000 order by avg(salary) desc
# 五.limit 限制查詢條數(shù) (應用在分頁)
	""" limit m,n m代表從第幾條數(shù)據(jù)開始查, n 代表查幾條  m=0 代表的是第一條數(shù)據(jù)"""
	select * from employee limit 0,10   # 0代表的是第一條數(shù)據(jù)
	select * from employee limit 10,10  # 10代表的是第十一條數(shù)據(jù)
	select * from employee limit 20,10  # 20代表的是第二十一條數(shù)據(jù)
	# limit + num  num => 搜索的條數(shù)據(jù)
	select * from employee limit 1
	# 搜索這個表里面最后一條數(shù)據(jù)
	select * from employee order by id desc limit 1
	# 搜索這個表里面最后五條數(shù)據(jù)
	select * from employee order by id desc limit 5
	
# 六.mysql 當中可以使用正則表達式 (不推薦,效率低)
	select * from employee where  emp_name regexp ".*on$"; # mysql中無法識別?
	select * from employee where  emp_name regexp "^程.*";
	select * from employee where  emp_name regexp "^程.*金";
# `### part2  多表查詢
	# 1.內(nèi)聯(lián)接 :  inner join  :  兩表或者多表之間,把滿足條件的所有數(shù)據(jù)查詢出來 (多表之間共同擁有的數(shù)據(jù)會被查詢出來)
		# 兩表聯(lián)查
		select 字段 from 表1 inner join 表2 on 必要的關(guān)聯(lián)條件
		# 多表聯(lián)查
		select 字段 from 表1 inner join 表2 on 必要的關(guān)聯(lián)條件1 inner join 表3 on 必要的關(guān)聯(lián)條件2 
	select * from employee inner join department on employee.dep_id = department.id;
	# as 起別名
	select * from employee as e inner join department as d on e.dep_id = d.id;
	# 也可以省略as (不推薦)	
	select * from employee e inner join department d on e.dep_id = d.id;
	# where 寫法默寫是內(nèi)聯(lián)接( 等同于inner join )
	select * from employee,department where employee.dep_id = department.id;
	select * from employee as e ,department as d where e.dep_id = d.id;
	# 2.外聯(lián)接 :  left join左聯(lián)接  / right join 右聯(lián)接
	# (1)left  join左聯(lián)接 : 以左表為主,右表為輔,完整查詢左表所有數(shù)據(jù),右表沒有的數(shù)據(jù)補null
	select * from employee left join department on employee.dep_id = department.id;
	# (2)right join右聯(lián)接 : 以右表為主,左表為輔,完整查詢右表所有數(shù)據(jù),左表沒有的數(shù)據(jù)補null
	select * from employee right join department on employee.dep_id = department.id;
	# 3.全聯(lián)接 :  union
	select * from employee left join department on employee.dep_id = department.id
	union
	select * from employee right join department on employee.dep_id = department.id;

3. mysql_子查詢

# ### part3 子查詢 
	"""
	子查詢: 嵌套查詢
		(1) sql語句當中又嵌套了另外一條sql,用括號()進行包裹,表達一個整體
		(2) 一般用在from子句,where子句... 身后,表達一個條件或者一個表
		(3) 速度快慢: 單表查詢 > 聯(lián)表查詢 > 子查詢;
	"""

	# 一.找出平均年齡大于25歲以上的部門
	# (1) where
	select 
		d.id,d.name
	from 
		employee as e ,department as d
	where
		e.dep_id = d.id
	group by 
		d.id,d.name
	having
		avg(e.age) > 25
	# (2) inner join 
	select 
		d.id,d.name
	from 
		employee as e inner join department as d on e.dep_id = d.id
	group by 
		d.id,d.name
	having
		avg(e.age) > 25
	# (3) 子查詢
	# 1.先找出平均年齡大于25歲的部門id
	select dep_id from employee group by employee.dep_id having avg(age)>25; # 201 202
	# 2.通過部門的id找部門的名字
	select name from department where id in (201,202);
	# 3.綜合拼接:
	select id , name from department where id in (select dep_id from employee group by employee.dep_id having avg(age)>25);
	# 二.查看技術(shù)部門員工姓名
	# (1) 普通的where 查詢
select 
	e.id,e.name
from
	employee as e,department as d
where
	e.dep_id = d.id
	and
	d.name = "技術(shù)"
	# (2) inner join 
select 
	e.id,e.name
from
	employee as e inner join department as d on e.dep_id = d.id 
where
	d.name = "技術(shù)"
	# (3)子查詢
	# (1) 找技術(shù)部門對應的id
	select id from department where name = "技術(shù)";
	# (2) 通過id找員工姓名
	select name from employee where dep_id = 200;
	# (3) 綜合拼接
	select id,name from employee where dep_id = (select id from department where name = "技術(shù)");
	# 三.查看哪個部門沒員工
	# 聯(lián)表寫法
	select
		d.id,d.name
	from
		department as d left join employee as e on d.id = e.dep_id
	where
		e.dep_id is null	
	
	# 1.找員工在哪些部門 (200  201  202 204)
	select dep_id from employee  group by dep_id
	# 2.把不在該部門的員工找出來
	select  id  from department where id not in (200,201,202,204);
	# 3.綜合拼接
	select  id,name  from department where id not in (select dep_id from employee  group by dep_id);
	department;
	+------+--------------+
	| id   | name         |
	+------+--------------+
	|  200 | 技術(shù)         |
	|  201 | 人力資源     |
	|  202 | 銷售         |
	|  203 | 運營         |
	+------+--------------+
	employee;
	+----+------------+--------+------+--------+
	| id | name       | sex    | age  | dep_id |avg(age) 
	+----+------------+--------+------+--------+
	|  1 | egon       | male   |   18 |    200 |  18
	|  2 | alex       | female |   48 |    201 |  43
	|  3 | wupeiqi    | male   |   38 |    201 |  43
	|  4 | yuanhao    | female |   28 |    202 |  28
	|  5 | liwenzhou  | male   |   18 |    200 |  18
	|  6 | jingliyang | female |   18 |    204 |  18
	+----+------------+--------+------+--------+
	# 四.查詢大于平均年齡的員工名與年齡
	# 假設已經(jīng)知道了平均年齡;
	select name,age from employee where age > 30;
	# 計算平均年齡
	select avg(age) from employee;
	# 綜合拼接
	select name,age from employee where age > (select avg(age) from employee);
	
	# 五.把大于其本部門平均年齡的員工名和姓名查出來
	# 1.先計算本部門的平均年齡是多少
	select dep_id , avg(age) from employee  group by dep_id;	
	+--------+----------+
	| dep_id | avg(age) |
	+--------+----------+
	|    200 |  18.0000 |
	|    201 |  43.0000 |
	|    202 |  28.0000 |
	|    204 |  18.0000 |
	+--------+----------+
	# 2.把查詢的各部門平均年齡和employee進行聯(lián)表,變成一張大表,最后做單表查詢
	select 
		*
	from
		employee as t1 inner join (1號查詢出來的數(shù)據(jù)) as t2 on t1.dep_id = t2.dep_id
	# 3.綜合拼裝
select 
	*
from
	employee as t1 inner join (select dep_id , avg(age) as avg_age from employee  group by dep_id) as t2 on t1.dep_id = t2.dep_id
	# 4.最后做一次單表查詢,讓age > 平均值	
select 
	*
from
	employee as t1 inner join (select dep_id , avg(age) as avg_age from employee  group by dep_id) as t2 on t1.dep_id = t2.dep_id
where 
	age >avg_age
	
	# 六.查詢每個部門最新入職的那位員工  # 利用上一套數(shù)據(jù)表進行查詢;
	employee
	+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
	| id | emp_name   | sex    | age | hire_date  | post                                    | post_comment | salary     | office | depart_id |    max_date
	+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
	|  1 | egon       | male   |  18 | 2017-03-01 | 辦事處外交大使                      |              |    7300.33 |    401 |         1 | 2017-03-01
	|  2 | alex       | male   |  78 | 2015-03-02 | teacher                                 | NULL         | 1000000.31 |    401 |         1 | 2015-03-02
	|  3 | wupeiqi    | male   |  81 | 2013-03-05 | teacher                                 | NULL         |    8300.00 |    401 |         1 | 2015-03-02 
	|  4 | yuanhao    | male   |  73 | 2014-07-01 | teacher                                 | NULL         |    3500.00 |    401 |         1 | 2015-03-02
	|  5 | liwenzhou  | male   |  28 | 2012-11-01 | teacher                                 | NULL         |    2100.00 |    401 |         1 | 2015-03-02
	|  6 | jingliyang | female |  18 | 2011-02-11 | teacher                                 | NULL         |    9000.00 |    401 |         1 | 2015-03-02
	|  7 | jinxin     | male   |  18 | 1900-03-01 | teacher                                 | NULL         |   30000.00 |    401 |         1 | 2015-03-02
	|  8 | 成龍       | male   |  48 | 2010-11-11 | teacher                                 | NULL         |   10000.00 |    401 |         1 | 2015-03-02
	|  9 | 歪歪       | female |  48 | 2015-03-11 | sale                                    | NULL         |    3000.13 |    402 |         2 | 2017-01-27
	| 10 | 丫丫       | female |  38 | 2010-11-01 | sale                                    | NULL         |    2000.35 |    402 |         2 | 2017-01-27
	| 11 | 丁丁       | female |  18 | 2011-03-12 | sale                                    | NULL         |    1000.37 |    402 |         2 | 2017-01-27
	| 12 | 星星       | female |  18 | 2016-05-13 | sale                                    | NULL         |    3000.29 |    402 |         2 | 2017-01-27
	| 13 | 格格       | female |  28 | 2017-01-27 | sale                                    | NULL         |    4000.33 |    402 |         2 | 2017-01-27
	| 14 | 張野       | male   |  28 | 2016-03-11 | operation                               | NULL         |   10000.13 |    403 |         3 | 2016-03-11
	| 15 | 程咬金     | male   |  18 | 1997-03-12 | operation                               | NULL         |   20000.00 |    403 |         3 | 2016-03-11
	| 16 | 程咬銀     | female |  18 | 2013-03-11 | operation                               | NULL         |   19000.00 |    403 |         3 | 2016-03-11
	| 17 | 程咬銅     | male   |  18 | 2015-04-11 | operation                               | NULL         |   18000.00 |    403 |         3 | 2016-03-11
	| 18 | 程咬鐵     | female |  18 | 2014-05-12 | operation                               | NULL         |   17000.00 |    403 |         3 | 2016-03-11
	+----+------------+--------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+
	# 1.找各部門的最新入職的時間
	select post,max(hire_date) as max_date from employee group by post
	+-----------------------------------------+------------+
	| post                                    | max_date   |
	+-----------------------------------------+------------+
	| operation                               | 2016-03-11 |
	| sale                                    | 2017-01-27 |
	| teacher                                 | 2015-03-02 |
	| 辦事處外交大使             				  | 2017-03-01 |
	+-----------------------------------------+------------+
	# 2.把子查詢搜索出來的結(jié)果作為一張表和employee這個表做聯(lián)表,把max_date拼接在employee這個表中,變成一張大表,最后做一次單表查詢
	select 
		*
	from
		employee as t1 inner join (1號數(shù)據(jù)) as t2 on t1.post = t2.post
	where
		t1.hire_date = t2.max_date
	# 3.綜合拼裝
select 
	emp_name , max_date
from
	employee as t1 inner join (select post,max(hire_date) as max_date from employee group by post) as t2 on t1.post = t2.post
where
	t1.hire_date = t2.max_date

4. exists關(guān)鍵字

	# 七.帶EXISTS關(guān)鍵字的子查詢
	"""
	exists 關(guān)鍵字 , 表達存在 , 應用在子查詢中
		如果內(nèi)層sql , 能夠查到數(shù)據(jù), 返回True ,  外層sql執(zhí)行相應的sql語句
		如果內(nèi)層sql , 不能查到數(shù)據(jù), 返回False , 外層sql不執(zhí)行sql語句
	"""
	select * from employee where exists (select * from employee where id = 1);
	select * from employee where exists (select * from employee where id = 100000);
	
	"""
	總結(jié): 
		子查詢可以單獨作為臨時數(shù)據(jù),作為一張表或者一個字段,通過()進行包裹,表達一個整體;
		一般用在from,where,select.子句的后面
		可以通過查詢出來的數(shù)據(jù)和另外的表做聯(lián)表變成更大一張表,
		最后做單表查詢,達到目的;
	"""

5. 練習所需表數(shù)據(jù)

# 單表練習
#創(chuàng)建表
create table employee(
id int not null unique auto_increment,
emp_name varchar(20) not null,
sex enum('male','female') not null default 'male', #大部分是男的
age int(3) unsigned not null default 28,
hire_date date not null,
post varchar(50),
post_comment varchar(100),
salary double(15,2),
office int, #一個部門一個屋子
depart_id int
);

#三個部門:教學,銷售,運營
insert into employee(emp_name,sex,age,hire_date,post,salary,office,depart_id) values
('egon','male',18,'20170301','辦事處外交大使',7300.33,401,1), #以下是教學部
('alex','male',78,'20150302','teacher',1000000.31,401,1),
('wupeiqi','male',81,'20130305','teacher',8300,401,1),
('yuanhao','male',73,'20140701','teacher',3500,401,1),
('liwenzhou','male',28,'20121101','teacher',2100,401,1),
('jingliyang','female',18,'20110211','teacher',9000,401,1),
('jinxin','male',18,'19000301','teacher',30000,401,1),
('成龍','male',48,'20101111','teacher',10000,401,1),
('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是銷售部門
('丫丫','female',38,'20101101','sale',2000.35,402,2),
('丁丁','female',18,'20110312','sale',1000.37,402,2),
('星星','female',18,'20160513','sale',3000.29,402,2),
('格格','female',28,'20170127','sale',4000.33,402,2),
('張野','male',28,'20160311','operation',10000.13,403,3), #以下是運營部門
('程咬金','male',18,'19970312','operation',20000,403,3),
('程咬銀','female',18,'20130311','operation',19000,403,3),
('程咬銅','male',18,'20150411','operation',18000,403,3),
('程咬鐵','female',18,'20140512','operation',17000,403,3)
;

# 練習:where
# 1. 查詢部門是sale的所有員工姓名:
# 2. 部門是teacher , 收入大于10000的所有數(shù)據(jù)
# 3. 收入在1萬到2萬之間的所有員工姓名和收入
# 4. 收入不在1萬到2萬之間的所有員工姓名和收入
# 5. 查看崗位描述為NULL的員工信息
# 5. 查詢收入是3000 ,4000 ,5000,8300 所有員工的姓名和收入
# 6. 以on結(jié)尾的員工名搜一下
# 7. 統(tǒng)計員工一年的年薪
# 8. 查詢部門的種類
#練習:group
# 1. 查詢部門名以及各部門的平均薪資
# 2. 查詢部門名以及各部門的最高薪資
# 3. 查詢部門名以及各部門的最低薪資
# 4. 查詢公司內(nèi)男員工和女員工的個數(shù)
# 5. 查詢部門名以及部門包含的所有員工名字
#練習:having
# 1.查詢各崗位內(nèi)包含的員工個數(shù)小于2的崗位名、崗位內(nèi)包含員工名字、個數(shù)
# 2.查詢各崗位平均薪資小于10000的崗位名、平均工資
# 3.查詢各崗位平均薪資大于10000且小于20000的崗位名、平均工資
#練習:order by
# 1. 查詢所有員工信息,先按照age升序排序,如果age相同則按照hire_date降序排序
# 2. 查詢各崗位平均薪資大于10000的崗位名、平均工資,結(jié)果按平均薪資升序排列
# 3. 查詢各崗位平均薪資大于10000的崗位名、平均工資,結(jié)果按平均薪資降序排列

# 多表練習:
#建表
create table department(
id int,
name varchar(20) 
);
create table employee(
id int primary key auto_increment,
name varchar(20),
sex enum('male','female') not null default 'male',
age int,
dep_id int
);
#插入數(shù)據(jù)
insert into department values
(200,'技術(shù)'),
(201,'人力資源'),
(202,'銷售'),
(203,'運營');
insert into employee(name,sex,age,dep_id) values
('egon','male',18,200),
('alex','female',48,201),
('wupeiqi','male',38,201),
('yuanhao','female',28,202),
('liwenzhou','male',18,200),
('jingliyang','female',18,204)
;
# 查詢:
# 一.找出平均年齡大于25歲以上的部門
# 二.查看技術(shù)部門員工姓名
# 三.查看哪個部門沒員工
# 四.查詢大于平均年齡的員工名與年齡
# 五.把大于其本部門平均年齡的員工名和姓名查出來
# 六.查詢每個部門最新入職的那位員工  # 利用上一套數(shù)據(jù)表進行查詢;
# 七.帶EXISTS關(guān)鍵字的子查詢

6. 小練習

(1)表結(jié)構(gòu):

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-mK1eZDP8-1640624422515)(assets/表結(jié)構(gòu).png)]

(2)黏貼如下sql,直接建表

# 1、創(chuàng)建表
# 創(chuàng)建班級表
create table class(
cid int primary key auto_increment,
caption varchar(32) not null
);
# 創(chuàng)建學生表
create table student(
sid int primary key auto_increment,
gender char(1) not null,
class_id int not null,
sname varchar(32) not null,
foreign key(class_id) references class(cid) on delete cascade on update cascade
);
# 創(chuàng)建老師表
create table teacher(
tid int primary key auto_increment,
tname varchar(32) not null
);
# 創(chuàng)建課程表
create table course(
cid int primary key auto_increment,
cname varchar(32) not null,
teacher_id int not null,
foreign key(teacher_id) references teacher(tid) on delete cascade on update cascade
);
# 創(chuàng)建成績表
create table score(
sid int primary key auto_increment,
student_id int not null,
course_id int not null,
num int not null,
foreign key(student_id) references student(sid) on delete cascade on update cascade,
foreign key(course_id) references course(cid) on delete cascade on update cascade
);

# 2、插入記錄
# 班級表插入記錄
insert into class values
('1', '三年二班'), 
('2', '三年三班'), 
('3', '一年二班'), 
('4', '二年一班');
# 學生表插入記錄
insert into student values
('1', '男', '1', '理解'), 
('2', '女', '1', '鋼蛋'), 
('3', '男', '1', '張三'), 
('4', '男', '1', '張一'), 
('5', '女', '1', '張二'), 
('6', '男', '1', '張四'), 
('7', '女', '2', '鐵錘'),
('8', '男', '2', '李三'), 
('9', '男', '2', '李一'), 
('10', '女', '2', '李二'), 
('11', '男', '2', '李四'), 
('12', '女', '3', '如花'), 
('13', '男', '3', '劉三'), 
('14', '男', '3', '劉一'), 
('15', '女', '3', '劉二'), 
('16', '男', '3', '劉四');
# 老師表插入記錄
insert into teacher values
('1', '張磊'), 
('2', '李平'), 
('3', '劉海燕'), 
('4', '朱云海'), 
('5', '李春秋');
# 課程表插入記錄
insert into course values
('1', '生物', '1'), 
('2', '物理', '2'), 
('3', '體育', '3'), 
('4', '美術(shù)', '2');
# 成績表插入記錄
insert into score values
('1', '1', '1', '10'), 
('2', '1', '2', '9'), 
('3', '1', '3', '76'),
('5', '1', '4', '66'), 
('6', '2', '1', '8'), 
('8', '2', '3', '68'), 
('9', '2', '4', '99'), 
('10', '3', '1', '77'), 
('11', '3', '2', '66'), 
('12', '3', '3', '87'), 
('13', '3', '4', '99'), 
('14', '4', '1', '79'), 
('15', '4', '2', '11'), 
('16', '4', '3', '67'), 
('17', '4', '4', '100'), 
('18', '5', '1', '79'), 
('19', '5', '2', '11'), 
('20', '5', '3', '67'), 
('21', '5', '4', '100'), 
('22', '6', '1', '9'), 
('23', '6', '2', '100'), 
('24', '6', '3', '67'), 
('25', '6', '4', '100'), 
('26', '7', '1', '9'), 
('27', '7', '2', '100'), 
('28', '7', '3', '67'), 
('29', '7', '4', '88'), 
('30', '8', '1', '9'), 
('31', '8', '2', '100'), 
('32', '8', '3', '67'),
('33', '8', '4', '88'), 
('34', '9', '1', '91'), 
('35', '9', '2', '88'), 
('36', '9', '3', '67'), 
('37', '9', '4', '22'), 
('38', '10', '1', '90'), 
('39', '10', '2', '77'), 
('40', '10', '3', '43'), 
('41', '10', '4', '87'), 
('42', '11', '1', '90'), 
('43', '11', '2', '77'), 
('44', '11', '3', '43'), 
('45', '11', '4', '87'), 
('46', '12', '1', '90'), 
('47', '12', '2', '77'), 
('48', '12', '3', '43'), 
('49', '12', '4', '87'), 
('52', '13', '3', '87');

(3)練習題目

1、查詢所有的課程的名稱以及對應的任課老師姓名
2、查詢學生表中男女生各有多少人
3、查詢物理成績等于100的學生的姓名
4、查詢平均成績大于八十分的同學的姓名和平均成績
5、查詢所有學生的學號,姓名,選課數(shù),總成績
6、 查詢姓李老師的個數(shù)
7、 查詢沒有報李平老師課的學生姓名
8、 查詢物理課程的分數(shù)比生物課程的分數(shù)高的學生的學號
9、 查詢沒有同時選修物理課程和體育課程的學生姓名
10、查詢掛科超過兩門(包括兩門)的學生姓名和班級
11、查詢選修了所有課程的學生姓名
12、查詢李平老師教的課程的所有成績記錄
13、查詢?nèi)繉W生都選修了的課程號和課程名
14、查詢每門課程被選修的次數(shù)
15、查詢只選修了一門課程的學生學號和姓名
16、查詢所有學生考出的成績并按從高到低排序(成績?nèi)ブ兀?
17、查詢平均成績大于85的學生姓名和平均成績
18、查詢生物成績不及格的學生姓名和對應生物分數(shù)
19、查詢在所有選修了李平老師課程的學生中,這些課程(李平老師的課程,不是所有課程)平均成績最高的學生姓名
20、查詢每門課程成績最好的課程id、學生姓名和分數(shù)
21、查詢不同課程但成績相同的課程號、學生號、成績 
22、查詢沒學過“李平”老師課程的學生姓名以及選修的課程名稱 
23、查詢所有選修了學號為2的同學選修過的一門或者多門課程的同學學號和姓名 
24、任課最多的老師中學生單科成績最高的課程id、學生姓名和分數(shù)

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • 教你怎么用python爬取愛奇藝熱門電影

    教你怎么用python爬取愛奇藝熱門電影

    突然心血來潮想看看電影,特地整理了這篇文章,文中有非常詳細的代碼示例,對正在學習python爬蟲的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • python編碼最佳實踐之總結(jié)

    python編碼最佳實踐之總結(jié)

    python編碼最佳實踐之總結(jié),幫助大家整理了python編碼最佳實踐的相關(guān)知識點,重點從性能角度出發(fā)對python的一些慣用法做一個簡單總結(jié),感興趣的小伙伴們可以參考一下
    2016-02-02
  • Python生成器與迭代器詳情

    Python生成器與迭代器詳情

    這篇文章主要介紹了Python生成器與迭代器,現(xiàn)在可以通過生成器來直接創(chuàng)建一個列表,是由于內(nèi)存的限制,表的容量肯定是有限的,果我們需要一個包含幾百個元素的列表,是每次訪問的時候只訪問其中的幾個,剩下的元素不使用就很浪費內(nèi)存空間,下面來了解具體內(nèi)容
    2021-11-11
  • NetWorkX使用方法及nx.draw()相關(guān)參數(shù)解讀

    NetWorkX使用方法及nx.draw()相關(guān)參數(shù)解讀

    這篇文章主要介紹了NetWorkX使用方法及nx.draw()相關(guān)參數(shù)解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 基于python實現(xiàn)音樂播放器代碼實例

    基于python實現(xiàn)音樂播放器代碼實例

    這篇文章主要介紹了基于python實現(xiàn)音樂播放器代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • python深度學習tensorflow入門基礎教程示例

    python深度學習tensorflow入門基礎教程示例

    這篇文章主要為大家介紹了python深度學習tensorflow入門基礎教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Python約瑟夫生者死者小游戲?qū)嵗v解

    Python約瑟夫生者死者小游戲?qū)嵗v解

    在本篇文章里小編給大家分享的是一篇關(guān)于Python約瑟夫生者死者小游戲?qū)嵗v解內(nèi)容,有興趣的朋友們可以測試學習下。
    2021-01-01
  • Python內(nèi)置庫之webbrowser模塊用法詳解

    Python內(nèi)置庫之webbrowser模塊用法詳解

    webbrowser模塊是Python自帶的標準庫,無需安裝,可以直接在Python中使用該模塊來打開網(wǎng)頁、PDF文件等,本文給大家詳細介紹了Python webbrowser模塊用法,需要的朋友可以參考下
    2023-08-08
  • Python實現(xiàn)單例模式的5種方法

    Python實現(xiàn)單例模式的5種方法

    單例模式應該是應用最廣泛,實現(xiàn)最簡單的一種創(chuàng)建型模式。本文詳細的介紹了Python實現(xiàn)單例模式的5種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Python爬蟲JSON及JSONPath運行原理詳解

    Python爬蟲JSON及JSONPath運行原理詳解

    這篇文章主要介紹了Python爬蟲JSON及JSONPath運行原理詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06

最新評論