python爬虫学习笔记-SQL学习
Sql概述
-
先來看一個例子:小王第一次使用數(shù)據(jù)庫,然后跟數(shù)據(jù)庫來了個隔空對話
- 其實,我們想一想,mysql是一個軟件,它有它自己一套的管理規(guī)則,我們想要跟它打交道,就必須遵守它的規(guī)則,如果我想獲取數(shù)據(jù),它自己有一套規(guī)則,這個規(guī)則就是SQL。
-
什么是sql?
- SQL : 結(jié)構(gòu)化查詢語言(Structured Query Language)簡稱SQL,是一種特殊目的的編程言,是一種數(shù)據(jù)庫查詢和程序設(shè)計語言,SQL語言主要用于存取數(shù)據(jù)、查詢數(shù)據(jù)、更新數(shù)據(jù)和管理關(guān)系數(shù)據(jù)庫系統(tǒng),SQL語言由IBM開發(fā)。
- 其實簡單的說,就是你發(fā)送給他能識別的暗號,他懂了就會給你返回數(shù)據(jù)。
- 注意:
- 對于關(guān)系型數(shù)據(jù)庫而言,SQL語句是通用的,學會了一種,其他只是一些細微的差別,畢竟人家數(shù)據(jù)庫也是一個種族,語言是通用的,只不過有些存在方言的差別。
-
sql的分類
-
SQL語言分為3種類型:
1、DDL語句 數(shù)據(jù)庫定義語言: 數(shù)據(jù)庫、表、視圖、索引、存儲過程,例如CREATE DROP ALTER
2、DML語句 數(shù)據(jù)庫操縱語言: 插入數(shù)據(jù)INSERT、刪除數(shù)據(jù)DELETE、更新數(shù)據(jù)UPDATE、查詢數(shù)據(jù)SELECT
3、DCL語句 數(shù)據(jù)庫控制語言: 例如控制用戶的訪問權(quán)限GRANT、REVOKE
-
庫表操作DDL
庫的增刪改查
這里的庫指的數(shù)據(jù)庫,也就是我們所謂的那個文件夾,一般情況下,我們在開發(fā)項目前,會先設(shè)計數(shù)據(jù)庫中相關(guān)表結(jié)構(gòu),一個項目中所有表都會放在同一個文件夾下,對于庫的操作屬于SQL分類中的DDL,也就是數(shù)據(jù)庫定義語言。
-
創(chuàng)建數(shù)據(jù)庫
- create database 倉庫名;
-
創(chuàng)建數(shù)據(jù)庫并制定編碼:utf8設(shè)置好后,就表示數(shù)據(jù)庫中可以存儲中文數(shù)據(jù)
- create database 倉庫名 charset utf8;
-
使用數(shù)據(jù)庫
- use 倉庫名;
-
查看所有數(shù)據(jù)庫和單獨常看當前數(shù)據(jù)庫
- show databases; #查看當前所有的數(shù)據(jù)倉庫 select database(); #查看當前使用的是哪一個數(shù)據(jù)倉庫 #注意:sql語句不區(qū)分大小寫,但是sql中使用的標識符盡量控制大小寫
-
修改數(shù)據(jù)庫
- 數(shù)據(jù)庫的名稱一旦創(chuàng)建好之后就無法修改
- 修改數(shù)據(jù)庫一般只修改編碼
- alter database 倉庫名 charset gbk #將數(shù)據(jù)的字符集編碼修改成了gbk
-
刪除數(shù)據(jù)庫
- drop database 倉庫名;
表的增刪改查
創(chuàng)建表
- create table 表名(字段名1 類型(寬度) 約束條件,字段名2 類型(寬度) 約束條件,字段名3 類型(寬度) 約束條件, ); 注意:1.字段名不能重復2.寬度和約束條件可選3.字段名和類型是必須的
查看表結(jié)構(gòu)
- desc 表名;
數(shù)據(jù)類型
**字符串:**顧名思義,就是存儲的一連串的字符,例如文字
?
數(shù)值型:常用的有:int ,double, float
-
整數(shù)型:int 基本int能夠處理日常工作中大部分整數(shù)存儲問題
-
小數(shù)型:double float
- 強調(diào)一下float(5,2)其中5代表總長度,2代表小數(shù)長度,這個意思是整數(shù)是3位,小數(shù)是2位
-
日常工作中float足以解決小數(shù)問題了。
日期類型
-
now()函數(shù):返回當前系統(tǒng)時間
-
date:年月日
-
time:時分秒
-
datetime:年月日時分秒
ENUM和SET類型:
- 這里的類似于下拉字段,在進行數(shù)據(jù)插入的時候,必須選擇事先設(shè)置的內(nèi)容
- 對于set而言,可以多選,但是enum只能單選
約束條件
-
為了防止不符合規(guī)范的數(shù)據(jù)進入數(shù)據(jù)庫,在用戶對數(shù)據(jù)進行插入、修改、刪除等操作時,DBMS自動按照一定的約束條件對數(shù)據(jù)進行監(jiān)測,使不符合規(guī)范的數(shù)據(jù)不能進入數(shù)據(jù)庫,以確保數(shù)據(jù)庫中存儲的數(shù)據(jù)正確、有效、相容。
-
約束條件與數(shù)據(jù)類型的寬度一樣,都是可選參數(shù),主要分為以下幾種:
- NOT NULL :非空約束,指定某列不能為空;
- DEFAULT:默認值
- UNIQUE : 唯一約束,指定某列或者幾列組合不能重復
- PRIMARY KEY :主鍵,指定該列的值可以唯一地標識該列記錄
- FOREIGN KEY :外鍵,指定該行記錄從屬于主表中的一條記錄,主要用于參照完整性
-
not null:不為空,當你設(shè)置一個字段時,不允許它為空,可以使用此約束條件
- create table t1 (id int,name varchar(20) not null#name字段的值不可以為空 )
-
default 默認值
- 例如:對于性別一列,如果大部分都是男性,可以設(shè)置成默認值,不填則取默認值,填寫了則覆蓋默認值
- create table t2 (id int,name varchar(20),sex char(10) DEFAULT('male'))
-
unique 唯一:當有一列字段你不想讓它有重復值時,可以設(shè)置為唯一
- 聯(lián)合唯一:只有當你設(shè)置的這些字段同時重復時才會報錯
-
primary key
-
主鍵為了保證表中的每一條數(shù)據(jù)的該字段都是表格中的唯一值。換言之,它是用來獨一無二地確認一個表格中的每一行數(shù)據(jù)。
-
主鍵可以包含一個字段或多個字段。當主鍵包含多個字段時,稱為組合鍵 (Composite Key),也可以叫聯(lián)合主鍵。
-
單字段主鍵:
- create table t5 (id int PRIMARY KEY , #主鍵約束 name varchar(20),salary float(6,2) )
-
聯(lián)合主鍵:
- create table t6 (id int, #主鍵約束 name varchar(20),salary float(6,2),PRIMARY KEY(id,name) #聯(lián)合主鍵 )
-
-
auto_increment 自增字段:
-
對于主鍵id而言,往往我們可以設(shè)置為自增字段,不用手動填寫
- create table t7 (id int PRIMARY KEY auto_increment, #主鍵約束 name varchar(20) )
-
-
foreign key
-
思考:
-
假設(shè)我們要描述所有公司的員工,需要描述的屬性有這些 : 姓名,年齡,性別,部門,部門描述
-
公司有3個部門,但是有1個億的員工,那意味著部門和部門描述這兩個字段需要重復存儲,部門名字和部門描述內(nèi)容越長,越浪費內(nèi)存,如何處理呢?
- 解決方法: 我們完全可以定義一個部門表然后讓員工信息表關(guān)聯(lián)該表,如何關(guān)聯(lián),即foreign key
-
foreign key(當前表中建立關(guān)系的外鍵字段) references 被關(guān)聯(lián)表名(id)
- #創(chuàng)建兩張表: #被關(guān)聯(lián)表:dep: create table dep(id int primary key auto_increment,dep_name varchar(16),dep_desc varchar(255));#關(guān)聯(lián)表:emp: create table emp(id int primary key auto_increment,name varchar(6),age int,gender enum('male','female'),dep_id int not null,foreign key(dep_id) references dep(id));
-
-
級聯(lián)刪除,級聯(lián)更新
-
兩張表建立關(guān)聯(lián)之后,如果部門表某個部門的砍掉了,那對應的人員表中的那些部門的人員相應的該怎么處理呢?可以保存,也可以隨之一起刪除.
-
如果要保證兩表一致,則需要在設(shè)置外鍵時添加on delete cascade
-
如果部門id更新了,要一起更新的話,則添加on update cascade
- #被關(guān)聯(lián)表:dep: create table dep(id int primary key auto_increment,dep_name varchar(16),dep_desc varchar(255));#關(guān)聯(lián)表:emp: create table emp(id int primary key auto_increment,name varchar(6),age int,gender enum('male','female'),dep_id int not null,foreign key(dep_id) references dep(id) on delete cascade on update cascade )
-
-
-
修改表
-
修改表名
- alter table 舊表名 rename 新表名
-
修改表字段的數(shù)據(jù)類型
- alter table 表名 modify 字段 新數(shù)據(jù)類型
-
修改表字段名
- alter table 表名 change 舊字段名 新字段名 新數(shù)據(jù)類型
-
新增字段
- alter table 表名 add 新增字段名 數(shù)據(jù)類型 約束條件#約束條件可選
-
刪除字段
- alter table 表名 drop 字段名
-
查看所有的表
- show tables
刪除表
drop table 表名記錄操作DML
插入數(shù)據(jù)
-
插入完整的數(shù)據(jù)
- insert into 表名 (字段1,字段2) values ('字段1的值','字段2的值')create table new_dep(id int PRIMARY KEY auto_increment,dep_name varchar(10),dep_desc varchar(100)
);
- #給指定字段插入數(shù)據(jù) insert into new_dep (dep_name,dep_desc) values ('yanfabu','ssss'); insert into new_dep (dep_name,dep_desc) values ('銷售部','ssss');
-
插入多條數(shù)據(jù)
- insert into 表名 values ('xx1','xx2'),(xx1,xx2) #插入幾條數(shù)據(jù)就寫幾個括號
更新數(shù)據(jù)
-
單獨更新一個字段
- update 表名 set 字段名 = 字段值 where 條件
-
更新多個字段
- update 表名 set 字段1 = 字段值1,字段2 = 字段值2 where 條件
刪除數(shù)據(jù)
-
刪除指定數(shù)據(jù)數(shù)據(jù)
- delete from 表名 where 條件
-
刪除表中所有數(shù)據(jù)
- #不加where條件就是清空表,一定要慎重使用delete # 如果有自增id,新增的數(shù)據(jù),仍然是以刪除前的最后一樣作為起始。 delete from 表名# 數(shù)據(jù)量大,刪除速度比上一條快,且直接從零開始 truncate table t1;
查詢數(shù)據(jù)
單表查詢
-
準備數(shù)據(jù)
- #創(chuàng)建員工表,字段為: #id自增,員工名不能為空,性別只可以為male或者female且不能為空,默認值為male #age不能為空,默認值28,入職日期只顯示年月日,職位名稱,工資保留兩位小數(shù),辦公室門牌號,部門id create table emp (id int PRIMARY KEY auto_increment,name char(20) not null,gender enum('male','female') default 'male',age int not null default 28,hire_date date,job_title varchar(30),salary float(10,2),office_num int,dep_id int )數(shù)據(jù) insert into emp (name,gender,age,hire_date,job_title,salary,office_num,dep_id)values ('weiwei','male',78,'20150302','teacher',1000000.31,401,1), ('lala','male',81,'20130305','teacher',8300,401,1), ('zhangsan','male',73,'20140701','teacher',3500,401,1), ('liulaogen','male',28,'20121101','teacher',2100,401,1), ('aal','female',18,'20110211','teacher',9000,401,1), ('zhugelang','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)
-
查詢所有字段信息
- select * from 表名; #星華表示的是通配符,表示所有的字段
-
查詢指定字段信息
- select 字段1,字段2 from 表名 select name,salary from emp #查看所有員工的姓名和薪資的信息
-
通過四則運算查詢
- #查看員工的姓名和其年薪 select name,salary*12 from emp #as可以給列起一個別名,as關(guān)鍵字可以省略 select name,salary*12 as year_salary from emp
-
為庫表重命名
- select * from emp as e #e就是emp的別名
-
條件查詢where語句
-
單條件查詢
- #查詢銷售員工的姓名和薪資 select name,salary from emp where job_title = 'sale'
-
多條件查詢1:結(jié)合and
- #查詢薪資大于1w的老師對應的名字和薪資 select name,salary from emp where job_title = 'teacher' and salary > 10000
-
多條件查詢2:結(jié)合or
- #查詢薪資為3,3.5,4,9k的員工名字和具體薪資 select name,salary from emp where salary = 3000 or salary = 3500 or salary = 4000 or salary = 9000
-
多條件查詢3:結(jié)合between…and
- #查詢薪資范圍在1000-5000之間的員工名字和具體信息 select name,salary from emp where salary between 1000 and 5000
-
多條件查詢4:結(jié)合in
- #查詢薪資為3,3.5,4,9k的員工名字和具體薪資 select name,salary from emp where salary in (3000,3500,4000,9000)
-
多條件查詢5:結(jié)合not in
- select name,salary from emp where salary not in (3000,3500,4000,9000)
-
模糊查詢like
-
通配符%:表示多個字符
- select * from emp where name like 'a%'
-
通配符_:表示一個字符
- select * from emp where name like 'zhang___'
-
-
-
分組查詢:group by
-
簡單的分組查詢
- 注意:使用group by的查詢字段必須是分組字段(或者是其他字段的聚合形式),否則會出錯,想要獲取其他字段信息,可以借助于聚合函數(shù)
- select job_title from emp group by job_title
-
分組聚合
- #查看每一個崗位的人數(shù) #count()使用來做計數(shù)操作 select job_title, count(id)from emp group by job_title#計算男女員工的平均年齡 select gender,avg(age) from emp group by gender#計算不同崗位員工的平均薪資 select job_title,avg(salary) from emp group by job_title#查看男女員工的最大,最小年級和整體年齡 select gender,max(age),min(age),sum(age) from emp group by gender
-
having子句
-
where 與 having的區(qū)別:
- where 是針對分組之前的字段內(nèi)容進行過濾,而having是針對分組后的
-
注意:having后面的條件字段只可以是分組后結(jié)果中存在的字段名,否則會報錯!
- #查看銷售崗位的平均薪資 select job_title,avg(salary) from emp GROUP BY job_title having job_title = 'sale'
-
-
-
排序:order by
-
升序: order by 字段 asc(默認升序,可以不寫)
-
降序: order by 字段 desc
-
單列排序:
- select * from emp order by salary
-
多列排序:越前面的列優(yōu)先級越高
- select * from emp order by salary desc
-
-
limit
-
顯示前三條數(shù)據(jù)
- select * from emp limit 3
-
從0開始,先查出第一條,然后包含這條再往后查5條
- select * from emp limit 0,5
-
從第3開始,即先查出第4條,然后包含這條再往后查7條
- select * from emp limit 3,7
-
-
使用正則:regexp
- # ^ 以什么開頭 select id,name,job_title from emp where name regexp "^a"; # $ 以什么結(jié)尾 select id,name,job_title from emp where name regexp "san$"; # 匹配指定字符內(nèi)容 select id,name,job_title from emp where name regexp "i{1}";
多表查詢
思考:沒有主外鍵關(guān)聯(lián),是否可以使得兩張表產(chǎn)生關(guān)聯(lián)關(guān)系?
根據(jù)指定條件將兩張表中的數(shù)據(jù)進行合并,然后在合并后的結(jié)果表中進行數(shù)據(jù)的查詢
-
準備數(shù)據(jù)
- create table dep(id int primary key,name char(20) ); create table emp(id int primary key auto_increment,name char(20),sex enum("male","female") not null default "male",age int,dep_id int );# 插入數(shù)據(jù) insert into dep values (200,'技術(shù)'), (201,'人力資源'), (202,'銷售'), (203,'運營');insert into emp(name,sex,age,dep_id) values ('ailsa','male',18,200), ('lala','female',48,201), ('huahua','male',38,201), ('zhangsan','female',28,202), ('zhaosi','male',18,200), ('shenteng','female',18,204) ;
-
內(nèi)連接:
- 兩張表公共的部分,必須同時有,沒有就不顯示
- #語法;select * from 表1 inner join 表2 on 合并條件 select * from emp inner join dep on emp.dep_id = dep.id
-
外連接之左連接
- 外連接之左連接
- 以左表為主表,根據(jù)左表數(shù)據(jù)匹配右表,左表數(shù)據(jù)是全的,而右表若匹配不上則為null
- 外連接之左連接
- select * from emp left join dep on emp.dep_id = dep.id
-
外連接之右連接
- 以右表為主表,根據(jù)右表數(shù)據(jù)匹配左表,右表數(shù)據(jù)是全的,而左表若匹配不上則為null
- select * from emp right join dep on emp.dep_id = dep.id
-
符合條件的多表聯(lián)查
- #示例1:找出年齡大于25歲的員工名字以及員工所在的部門名稱 select d.name,e.name from emp as e inner join dep as d on e.dep_id = d.id where age > 25#示例2:找出年齡大于25歲的員工名字以及員工所在的部門名稱,并且以age字段的升序方式顯示 select * from emp as e inner join dep as d on d.id = e.dep_id where age > 25 order by age
-
子查詢:子查詢是將一個查詢語句嵌套在另一個查詢語句中
-
帶in關(guān)鍵字的子查詢
- 查詢平均年齡在25歲以上的部門名
- #下述sql返回的就是201和202
select dep_id from emp group by dep_id having avg(age) > 25#整合后的子查詢語句 select name from dep where id in (200,201)
select name from dep where id in
(select dep_id from emp group by dep_id having avg(age) > 25
)
- 查看技術(shù)部員工姓名
- #獲取部門編號200 select id from dep where name = '技術(shù)'#子查詢 select name from emp where dep_id in (200) select name from emp where dep_id in (select id from dep where name = '技術(shù)' )
-
帶比較運算符的子查詢(比較運算符: =、!=、>、>=、<、<=、<>)
-
查詢大于所有人平均年齡的員工名字與年齡
- select id,name from emp where age > xx #xx就是所有員工的平均年齡 #如何獲取所有員工的平均年齡 select avg(age) from emp#整合后的子查詢 select id,name from emp where age > (select avg(age) from emp)
-
-
綜合練習
-
準備數(shù)據(jù)
- create table class (cid int PRIMARY KEY,caption varchar(32) not null ) INSERT INTO class VALUES (1, '三年二班'), (2, '三年三班'), (3, '一年二班'), (4, '二年九班');CREATE TABLE teacher(tid int PRIMARY KEY,tname varchar(32) NOT NULL ) INSERT INTO teacher VALUES (1, '張磊老師'), (2, '李平老師'), (3, '劉海燕老師'), (4, '朱云海老師'), (5, '李杰老師');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) ) INSERT INTO course VALUES (1, '生物', 1), (2, '物理', 2), (3, '體育', 3), (4, '美術(shù)', 2);CREATE TABLE student(sid int(11) PRIMARY KEY AUTO_INCREMENT,gender char(1) NOT NULL,class_id int(11) NOT NULL,sname varchar(32) NOT NULL,FOREIGN KEY (class_id) REFERENCES class (cid) ) 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, '劉四'); 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 (course_id) REFERENCES course (cid),FOREIGN KEY (student_id) REFERENCES student(sid) ) INSERT INTO score VALUES (1, 1, 1, 10), (2, 1, 2, 9), (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);
-
題目
- 1、查詢所有的課程的名稱以及對應的任課老師姓名 select c.cname,t.tname from course as c inner join teacher as t ONc.teacher_id = t.tid2、查詢學生表中男女生各有多少人 select gender,count(sid) from student GROUP BY gender 3、查詢物理成績等于100的學生的姓名 select t.sname,c.cname,s.num from student as t inner join score as s ON t.sid = s.student_id inner join course as c ON c.cid = s.course_id where c.cname = '物理' and s.num = 1004、查詢平均成績大于八十分的同學的姓名和平均成績 #查詢平均成績大于八十分的同學的姓名和平均成績 #涉及到的字段:成績,學生名字 #注意:分組條件為一張表的主鍵ID,則查詢字段可以是該表中的任意字段 select t.sid,avg(num),t.sname from student as t inner join score as s on s.student_id = t.sid GROUP BY t.sid having avg(s.num) > 805、查詢所有學生的姓名,選課數(shù),總成績 select t.sname,count(course_id) as 科目數(shù),sum(s.num) as 總成績 from student as t inner join score as s on t.sid = s.student_id GROUP BY t.sid 6、 查詢姓李老師的個數(shù) select count(tid) from teacher where tname like '李%'7、 查詢沒有報李平老師課的學生姓名 #select sname from student where sname not in (報李平老師學生的名字)#報李平老師學生的名字 #select t1.sname from student as t1 inner join score as s on t1.sid = s.student_id inner join course as c on c.cid = s.course_id inner join teacher as t ON t.tid = c.teacher_id where t.tname = '李平老師'#整合的完成操作 select sname from student where sname not in (select t1.sname from student as t1 inner join score as s on t1.sid = s.student_id inner join course as c on c.cid = s.course_id inner join teacher as t ON t.tid = c.teacher_id where t.tname = '李平老師')8、 查詢物理課程比生物課程高的學生的學號 #1.求出每個學生的物理分數(shù) select s.student_id,s.num from score as s inner join course as c on s.course_id = c.cid where c.cname = '物理' #2.求出每個學生的生物分數(shù) select s.student_id,s.num from score as s inner join course as c on s.course_id = c.cid where c.cname = '生物' #假設(shè)t1就是上面物理對應的表,t2就是上面生物對應的表 select * from t1 inner join t2 on t1.student_id = t2.student_id where t1.num > t2.num #最終的整合 select t2.student_id from (select s.student_id,s.num from score as s inner join course as c on s.course_id = c.cid where c.cname = '物理') as t1 inner join (select s.student_id,s.num from score as s inner join course as c on s.course_id = c.cid where c.cname = '生物') as t2 on t1.student_id = t2.student_id where t1.num > t2.num9、 查詢只選修了物理課程或體育課程的學生姓名 #限制只能要求學生報物理或者報體育其中的一門,不能同時報這兩門 select t.sid,t.sname from student as t inner join score as s on t.sid = s.student_id inner join course as c on c.cid = s.course_id where c.cname = '物理' or c.cname = '體育' GROUP BY t.sid having count(c.cid) < 210、查詢掛科超過兩門(包括兩門)的學生姓名和班級id select s.sname,count(s1.sid) 個數(shù) from student s join score s1 on s.sid = s1.student_id where num < 60 group by sname having count(s1.sid)>=2;11、查詢選修了所有課程的學生姓名 #查詢選修了所有課程的學生姓名 select t.sname from student as t inner join score as s on t.sid = s.student_id GROUP BY t.sid having count(s.course_id) = 所有科目的數(shù)量 #查詢所有科目的數(shù)量 select count(cid) from course #整合 select t.sname from student as t inner join score as s on t.sid = s.student_id GROUP BY t.sid having count(s.course_id) = (select count(cid) from course ) 12、查詢李平老師教的課程的所有學生(id)的成績記錄 select s.student_id,c.cname,s.num from score s left join course c on s.course_id = c.cid left join teacher t on t.tid = c.teacher_id where t.tname = "李平老師";13、查詢?nèi)繉W生都選修了的課程號和課程名 select c.cid,c.cname from score as s inner join course as c ON s.course_id = c.cid GROUP BY c.cid having count(course_id) = (select count(sid) from student)14、查詢每門課程被選修的次數(shù) select c.cname,count(c.cid) from score as s inner join course as c ON s.course_id = c.cid GROUP BY c.cid 15、查詢只選修了一門課程的學生姓名和學號 select t.sname,t.sid from student as t inner join score as s on t.sid = s.student_id GROUP BY student_id HAVING count(s.course_id) = 116、查詢所有學生考出的成績并按從高到低排序(成績?nèi)ブ?#xff09; #查詢所有學生考出的成績并按從高到低排序(成績?nèi)ブ?#xff09; #DISTINCT對某一個查詢字段去重 select DISTINCT num from score ORDER BY num desc17、查詢平均成績大于85的學生姓名和平均成績 select t.sname,avg(num) from student as t inner join score as s on t.sid = s.student_id group by t.sid having avg(num) > 8518、查詢生物成績不及格的學生姓名和對應生物分數(shù) select t.sname,num from student as t inner join score as s on t.sid = s.student_id inner join course as c on c.cid = s.course_id where cname = '生物' and num < 60 19、查詢在所有選修了李平老師課程的學生中,這些課程(李平老師的課程,不是所有課程)平均成績最高的學生姓名 select t1.sname,avg(num) from student as t1 inner join score as s on t1.sid = s.student_id inner join course as c on c.cid = s.course_id inner join teacher as t on t.tid = c.teacher_id where t.tname = '李平老師' group by t1.sid order by avg(num) desc limit 1
總結(jié)
以上是生活随笔為你收集整理的python爬虫学习笔记-SQL学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实现LUT
- 下一篇: 巴菲特致股东的一封信:1987年