-- 就是一个查询语句中再嵌套一个查询语句-- 作用:子查询的结果能够返回给主查询,作为条件或者作为一张临时表-- 题目:查询员工表里面最高工资的员工信息select*from emp;-- 查询最高工资是多少max(列名)selectmax(salary)from emp;-- 对应员工的信息 怎么写?-- 换种问法:可以查询出工资为9000(最高工资)的员工信息select*from emp where salary =9000;select*from emp where salary =(selectmax(salary)from emp);select*from emp;select*from dept;-- 查询工资大于5000的员工在哪个部门-- 表连接-- 查询员工及员工所在部门select*from emp,dept where emp.dept_id = dept.id and salary >5000;-- 子查询写? 1,2,3select*from emp where salary>5000;select*from dept where id in(select dept_id from emp where salary >5000);select*from dept d,(select*from emp where salary >5000) e where d.id = e.dept_id;
3.作业
-- 1.使用嵌套语句查询选修课程名称为‘税收基础’的学员学号和姓名-- 2.使用嵌套语句查询选修课程编号为‘C2’的学员姓名和所属单位-- 3.使用嵌套语句查询不选修课程编号为‘C5’的学员姓名和所属单位-- 学生表CREATETABLE S(
SNO int,
SNAME VARCHAR(20),
SDD VARCHAR(50),
SA int);-- 课程表CREATETABLE C(
CNO VARCHAR(20)NOTNULLPRIMARYKEY,
CNAME varchar(20));-- 成绩表CREATETABLE SC(
SNO int,
CNO VARCHAR(20),
SCORE INT);-- 学生表INSERTINTO S(SNO,SNAME,SDD,SA)VALUES(2019001,'托马斯李','运营',26);INSERTINTO S(SNO,SNAME,SDD,SA)VALUES(2019002,'米高扬','管理',30);INSERTINTO S(SNO,SNAME,SDD,SA)VALUES(2019003,'蝙蝠侠','安防',22);INSERTINTO S(SNO,SNAME,SDD,SA)VALUES(2019004,'李嘉诚','投资',45);INSERTINTO S(SNO,SNAME,SDD,SA)VALUES(2019005,'雷军','开发',34);INSERTINTO S(SNO,SNAME,SDD,SA)VALUES(2019006,'周小川','管理',56);INSERTINTO S(SNO,SNAME,SDD,SA)VALUES(2019007,'陆奇','运营',36);INSERTINTO S(SNO,SNAME,SDD,SA)VALUES(2019008,'普京','安防',67);-- 课程表INSERTINTO C(CNO,CNAME)VALUES('C1','税收基础');INSERTINTO C(CNO,CNAME)VALUES('C2','金融工程');INSERTINTO C(CNO,CNAME)VALUES('C3','会计');INSERTINTO C(CNO,CNAME)VALUES('C4','统计学习方法');INSERTINTO C(CNO,CNAME)VALUES('C5','大数据');INSERTINTO C(CNO,CNAME)VALUES('C6','机器学习算法');-- 成绩表INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019001,'C2',80);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019002,'C2',78);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019003,'C1',89);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019003,'C5',60);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019004,'C4',90);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019005,'C1',87);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019005,'C2',75);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019005,'C3',80);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019005,'C4',90);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019005,'C5',86);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019005,'C6',88);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019006,'C1',99);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019006,'C2',61);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019007,'C1',62);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019007,'C2',78);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019007,'C3',77);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019007,'C4',69);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019007,'C5',98);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019007,'C6',88);INSERTINTO SC(SNO,CNO,SCORE)VALUES(2019008,'C1',78);select*from s;select*from sc;select*from c;-- 1.使用嵌套语句查询选修课程名称为‘税收基础’的学员学号和姓名/*
查询:学员学号和姓名
条件:选修课程名称为'税收基础'C
需要用到SC表 税收基础 C1
s表和c表是没有关系的,sc表
c表 税收基础对应的课程编号 cno = 'c1'
sc表找c1对应的sno(学生编号)哪几个学生学了c1(税收基础')
根据学生编号找到了学生信息 s表里面找sno = 003,005,006,007
sc表 找c1课程就能找到学生编号 s表 找003
*/select sno,sname from s;select*from c where cname ='税收基础';select*from sc where CNO ='c1';-- 知道了学员编号是不是就能查询到对应的学员信息select sno,sname from s where sno in(select sno from sc where cno =(select CNO from C where CNAME ='税收基础'));select S.SNO,SNAME from S,SC where S.SNO = SC.SNO and SC.CNO =(select CNO from C where CNAME ='税收基础');-- 2.使用嵌套语句查询选修课程编号为‘C2’的学员姓名和所属单位/*
查询学员姓名和所属单位 s表 sname,sdd
选修了'c2'课程 001 002 005 006 007
*/select sname,sdd from s where sno in(select sno from sc where cno ='c2');select*from sc whre cno ='C2';select SNAME,SDD from S where SNO in(select SNO from SC where CNO ='C2');-- 3.使用嵌套语句查询不选修课程编号为‘C5’的学员姓名和所属单位-- 03,05,07选了c5课程select*from sc where cno ='c5';select SNAME,SDD from S where SNO notin(select SNO from SC where CNO ='C5');
4.存储过程
-- 函数封装 封装sql语句 用的时候 使用存储名调用
-- 语法
/*
delimiter //
create PROCEDURE 存储名()
begin
select *from 表名;
END
*/-- 创建存储过程 无参数 无返回
delimiter //
create procedure test_1()
BEGIN
select *from student3;
END//-- 使用 call存储名()
call test_1()-- 有参数 in 传数据进来
delimiter //
create PROCEDURE test_2(in uname varchar(20))
BEGIN
select *from student3 where name = uname;
END//
call test_2('马云')-- 有返回 用out 输出数据
delimiter //
create PROCEDURE test_3(in uname varchar(20),out usex varchar(20))
BEGIN
select sex into usex from student3 where name = uname;
END//
call test_3('马云',@usex);
select @usex
5.用Python连接数据库
# 1.连接数据库# 2.操作数据库 创建游标 用游标 执行sql语句 获得结果# 3.关闭数据库
①导包
#pymysql安装方式 pip install pymysqlimport pymysql
②连接数据库
con = pymysql.connect(host='127.0.0.1',port=3306,user='root',password='xxx',database='you',charset='utf8')print(con)
③操作数据库, 创建游标
cur = con.cursor()# 写sql
sql ='select * from student3'# 执行sql
cur.execute(sql)
④查询几条数据
# fetchmany(指定数据) 查询所有数据fetchall 查询一条数据fetchone# result = cur.fetchall()# result = cur.fetchone()
result = cur.fetchmany(2)print(result)
⑤增加 修改 删除
con = pymysql.connect(host='127.0.0.1',port=3306,user='root',password='xxx',database='you',charset='utf8')
cur = con.cursor()# sql='delete from student3 where id = 8'
sql1 ='insert into student3(id,name) values(8,"aaa")'
cur.execute(sql1)
cur.execute('commit')# 别的地方 不会重新写 用哪部分改一下 封装函数
from class15.demo3 import MySQLDB
# a=MySQLDB('127.0.0.1',3306,'root','xxx','you','utf8')# b=a.get_one('select * from student3')# print(b)# 项目中写法 ini文件config
a = MySQLDB('db.ini','Test_db')
b = a.get_one('select * from student3')print(b)