最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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.查詢對應(yīng)的區(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 按照分組把對應(yīng)字段拼在一起;
	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ù) (應(yīng)用在分頁)
	""" 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ù)部門對應(yīng)的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
	+----+------------+--------+------+--------+
	# 四.查詢大于平均年齡的員工名與年齡
	# 假設(shè)已經(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)鍵字 , 表達存在 , 應(yīng)用在子查詢中
		如果內(nèi)層sql , 能夠查到數(shù)據(jù), 返回True ,  外層sql執(zhí)行相應(yīng)的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、查詢所有的課程的名稱以及對應(yīng)的任課老師姓名
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、查詢生物成績不及格的學生姓名和對應(yīng)生物分數(shù)
19、查詢在所有選修了李平老師課程的學生中,這些課程(李平老師的課程,不是所有課程)平均成績最高的學生姓名
20、查詢每門課程成績最好的課程id、學生姓名和分數(shù)
21、查詢不同課程但成績相同的課程號、學生號、成績 
22、查詢沒學過“李平”老師課程的學生姓名以及選修的課程名稱 
23、查詢所有選修了學號為2的同學選修過的一門或者多門課程的同學學號和姓名 
24、任課最多的老師中學生單科成績最高的課程id、學生姓名和分數(shù)

總結(jié)

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

相關(guān)文章

  • python3翻轉(zhuǎn)字符串里的單詞點的實現(xiàn)方法

    python3翻轉(zhuǎn)字符串里的單詞點的實現(xiàn)方法

    這篇文章主要介紹了python3翻轉(zhuǎn)字符串里的單詞點的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • 用uWSGI和Nginx部署Flask項目的方法示例

    用uWSGI和Nginx部署Flask項目的方法示例

    這篇文章主要介紹了用uWSGI和Nginx部署Flask項目的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • python非標準時間的轉(zhuǎn)換

    python非標準時間的轉(zhuǎn)換

    本文主要介紹了python非標準時間的轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Python基于socket實現(xiàn)TCP/IP客戶和服務(wù)器通信

    Python基于socket實現(xiàn)TCP/IP客戶和服務(wù)器通信

    本主要介紹了Python socket網(wǎng)絡(luò)編程TCP/IP服務(wù)器與客戶端通信的相關(guān)資料,這里對Scoket 進行詳解并創(chuàng)建TCP服務(wù)器及TCP 客戶端實例代碼,需要的朋友可以參考下
    2021-06-06
  • Matplotlib自定義坐標刻度的使用示例

    Matplotlib自定義坐標刻度的使用示例

    雖然matplotlib默認的坐標軸定位器與格式生成器可以滿足大部分需求,但是并非對每一幅圖都合適,本文主要介紹了Matplotlib自定義坐標刻度的使用示例,感興趣的可以了解一下
    2023-11-11
  • python通過配置文件共享全局變量的實例

    python通過配置文件共享全局變量的實例

    今天小編就為大家分享一篇python通過配置文件共享全局變量的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • python 進階學習之python裝飾器小結(jié)

    python 進階學習之python裝飾器小結(jié)

    這篇文章主要介紹了python 進階學習之python裝飾器小結(jié),本文通過場景分析給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Python?中10進制數(shù)與16進制數(shù)相互轉(zhuǎn)換問題

    Python?中10進制數(shù)與16進制數(shù)相互轉(zhuǎn)換問題

    這篇文章主要介紹了Python中10進制數(shù)與16進制數(shù)相互轉(zhuǎn)換,在Python中,我們可以使用內(nèi)置的hex()函數(shù)將10進制數(shù)轉(zhuǎn)換為16進制數(shù),需要的朋友可以參考下
    2023-05-05
  • python機器學習理論與實戰(zhàn)(四)邏輯回歸

    python機器學習理論與實戰(zhàn)(四)邏輯回歸

    這篇文章主要為大家詳細介紹了python機器學習理論與實戰(zhàn)第四篇,邏輯回歸的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 刪除pycharm鼠標右鍵快捷鍵打開項目的操作

    刪除pycharm鼠標右鍵快捷鍵打開項目的操作

    這篇文章主要介紹了刪除pycharm鼠標右鍵快捷鍵打開項目的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01

最新評論

筠连县| 中超| 来宾市| 靖边县| 通榆县| 大方县| 肃南| 霍州市| 长宁区| 蒙城县| 永修县| 巨野县| 嘉鱼县| 东平县| 富锦市| 庆元县| 虞城县| 黄山市| 托克托县| 新郑市| 西安市| 吉水县| 江源县| 昌吉市| 汉寿县| 四川省| 修水县| 新邵县| 田东县| 阿鲁科尔沁旗| 临西县| 绵阳市| 宜兴市| 定结县| 辉南县| 米脂县| 和政县| 太仓市| 调兵山市| 昆明市| 财经|