python 之 数据库(多表查询之连接查询、子查询、pymysql模块的使用)
10.10 多表连接查询
10.101 内连接
把两张表有对应关系的记录连接成一张虚拟表
select * from emp,dep; #连接两张表的笛卡尔积
select * from emp,dep where emp.dep_id = dep.id; # 不推荐用where连接表
select * from emp inner join dep on emp.dep_id = dep.id; #推荐
+----+-----------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+----+-----------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技术 |
+----+-----------+--------+------+--------+------+--------------+
#应用:
select * from emp,dep where emp.dep_id = dep.id and dep.name = "技术";
select * from emp inner join dep on emp.dep_id = dep.id where dep.name = "技术";
+----+-----------+------+------+--------+------+--------+
| id | name | sex | age | dep_id | id | name |
+----+-----------+------+------+--------+------+--------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技术 |
+----+-----------+------+------+--------+------+--------+
应用
10.102 左连接
在内连接的基础上,保留左边没有对应关系的记录
select * from emp left join dep on emp.dep_id = dep.id;
+----+------------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+----+------------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 6 | jingliyang | female | 18 | 204 | NULL | NULL |
+----+------------+--------+------+--------+------+--------------+
10.103 右连接
在内连接的基础上,保留右边没有对应关系的记录
select * from emp right join dep on emp.dep_id = dep.id;
+------+-----------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+------+-----------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技术 |
| NULL | NULL | NULL | NULL | NULL | 203 | 运营 |
+------+-----------+--------+------+--------+------+--------------+
10.104 全连接
在内连接的基础上,保留左、右边没有对应关系的记录,并去重
select * from emp left join dep on emp.dep_id = dep.id
union
select * from emp right join dep on emp.dep_id = dep.id;
+------+------------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+------+------------+--------+------+--------+------+--------------+
| 1 | egon | male | 18 | 200 | 200 | 技术 |
| 5 | liwenzhou | male | 18 | 200 | 200 | 技术 |
| 2 | alex | female | 48 | 201 | 201 | 人力资源 |
| 3 | wupeiqi | male | 38 | 201 | 201 | 人力资源 |
| 4 | yuanhao | female | 28 | 202 | 202 | 销售 |
| 6 | jingliyang | female | 18 | 204 | NULL | NULL |
| NULL | NULL | NULL | NULL | NULL | 203 | 运营 |
+------+------------+--------+------+--------+------+--------------+
补充:多表连接可以不断地与虚拟表连接
#查找各部门最高工资
select t1.* from emp as t1 inner join (select post,max(salary) as ms from emp group by post) as t2
on t1.post = t2.post
where t1.salary = t2.ms;
10.11 子查询
把一个查询语句用括号括起来,当做另外一条查询语句的条件去用,称为子查询
#查询技术部员工的名字
select emp.name from emp inner join dep on emp.dep_id = dep.id where dep.name="技术";#连接查询
select name from emp where dep_id =(select id from dep where name="技术"); #子查询
+-----------+
| name |
+-----------+
| egon |
| liwenzhou |
+-----------+
#查询平均年龄在25岁以上的部门名 #子查询
select name from dep where id in (select dep_id from emp group by dep_id having avg(age) > 25);
select dep.name from emp inner join dep on emp.dep_id = dep.id #连接查询
group by dep.name
having avg(age) > 25;
+--------------+
| name |
+--------------+
| 人力资源 |
| 销售 |
+--------------+
#查询每个部门最新入职的那位员工
select t1.id,t1.name,t1.post,t1.hire_date,t2.post,t2.max_date from (emp as t1) inner join
(select post,max(hire_date) as max_date from emp group by post) as t2 #拿到最大雇佣时间
on t1.post = t2.post
where t1.hire_date = t2.max_date;
+----+--------+-----------------------------------------+----
| id | name | post | hire_date | post | max_date |
+----+--------+-----------------------------------------+-----
| 1 | egon | 外交大使 | 2017-03-01 | 外交大使 | 2017-03-01 |
| 2 | alex | teacher | 2015-03-02 | teacher | 2015-03-02 |
| 13 | 格格 | sale | 2017-01-27 | sale | 2017-01-27 |
| 14 | 张野 | operation| 2016-03-11 | operation| 2016-03-11 |
+----+--------+-----------------------------------------+-----
exists( ):括号内的值存在时满足条件
select * from emp where exists (select id from dep where id > 3); #找到所有
10.12 pymysql模块的使用
10.121 pymysql查
import pymysql #pip3 install pymysql
conn=pymysql.connect( #连接
host='127.0.0.1',
port=3306,
user='root',
password='',
database='db2',
charset='utf8')
cursor=conn.cursor(pymysql.cursors.DictCursor)#以字典形式显示表的记录
rows=cursor.execute('show tables;') #1 显示受影响的行数(row),此处为有表的条数
print(rows)
rows=cursor.execute('select * from emp;') #18 此处rows为emp表内有记录的条数
print(rows)
print(cursor.fetchone()) #查看一条记录 一个字典{key:value}
print(cursor.fetchmany(2)) #查看多条记录 [{key:value},]
#print(cursor.fetchall()) #查看所有记录 强调:下一次查找是接着上一次查找的位置继续
cursor.scroll(0,'absolute') #绝对移动,以0位置为参照显示
print(cursor.fetchone())
cursor.scroll(1,'relative') #相对移动,相对当前位置移动1条记录
print(cursor.fetchone())
cursor.close()#光标
conn.close()
10.122 防止sql注入问题
在服务端防止sql注入问题:不要自己拼接字符串,让pymysql模块去拼接,pymysql拼接时会过滤非法字符
import pymysql
conn=pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='',
database='db2',
charset='utf8'
)
cursor=conn.cursor(pymysql.cursors.DictCursor)
inp_user=input('用户名>>:').strip() #inp_user=""
inp_pwd=input('密码>>:').strip() #inp_pwd=""
sql="select * from user where username=%s and password=%s"
print(sql) rows=cursor.execute(sql,(inp_user,inp_pwd))#输入的用户名和密码中的非法字符会被过滤掉
if rows:
print('登录成功')
else:
print('登录失败')
cursor.close()
conn.close()
10.123 pymysql增删改
import pymysql
conn=pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='',
database='db2',
charset='utf8')
cursor=conn.cursor(pymysql.cursors.DictCursor)
sql='insert into user(username,password) values(%s,%s)' #插入单行记录
rows=cursor.execute(sql,('EGON',''))
print(rows)
print(cursor.lastrowid) #显示当前最后一行的id
sql='insert into user(username,password) values(%s,%s)' #一次插入多行记录
rows=cursor.executemany(sql,[('lwz','123'),('evia','455'),('lsd','333')])
print(rows)
rows=cursor.execute('update user set username="alexSB" where id=2')#修改记录
print(rows)
conn.commit() # 只有commit提交才会完成真正的修改
cursor.close()
conn.close()
python 之 数据库(多表查询之连接查询、子查询、pymysql模块的使用)的更多相关文章
- MySQL数据库学习笔记(六)----MySQL多表查询之外键、表连接、子查询、索引
本章主要内容: 一.外键 二.表连接 三.子查询 四.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复 ...
- MySQL数据库学习笔记----MySQL多表查询之外键、表连接、子查询、索引
本章主要内容: 一.外键 二.表连接 三.子查询 四.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复 ...
- MS sql server 基础知识回顾(二)-表连接和子查询
五.表连接 当数据表中存在许多重复的冗余信息时,就要考虑将这些信息建在另一张新表中,在新表中为原表设置好外键,在进行数据查询的时候,就要使用到连接了,表连接就好像两根线,线的两端分别连接两张表的不同字 ...
- MySQL多表查询之外键、表连接、子查询、索引
MySQL多表查询之外键.表连接.子查询.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为 ...
- mysql学习笔记-- 多表查询之外键、表连接、子查询、索引
本章主要内容: 一.外键 二.表连接 三.子查询 四.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复 ...
- 数据库——SQL-SERVER练习(1)连接与子查询
一.实验准备 1.复制实验要求文件及“CREATE-TABLES.SQL”文件, 粘贴到本地机桌面. 2.启动SQL-SERVER服务. 3. 运行查询分析器, 点击菜单<文件>/< ...
- mysql表查询、多表查询(增强查询的使用)子查询、合并查询,外连接,mysql5种约束,自增长
一.查询加强 1.在mysql中,日期类型可以直接比较,需要注意格式 2.%:表示0到多个字符, _:表示单个字符 exp:显示第二个字符为大写O的所有员工的姓名和工资 select name fr ...
- ylb:SQL 表的高级查询-多表连接和子查询
ylbtech-SQL Server: SQL Server-表的高级查询-多表连接和子查询 SQL Server 表的高级查询-多表连接和子查询. 1,ylb:表的高级查询-多表连接和子查询 返回顶 ...
- oracle之复杂查询(下):子查询
复杂查询(下):子查询 8. 1 非关联子查询:返回的值可以被外部查询使用.子查询可以独立执行的(且仅执行一次). 8.1.1 单行单列子查询,子查询仅返回一个值,也称为标量子查询,采用单行比较运算符 ...
- mysql加强(6)~子查询简单介绍、子查询分类
一.子查询简单介绍 1.什么是子查询? 一个查询之中嵌套了其他的若干查询. 在使用select 语句查询时,有时候where的查询条件中的限制条件不是一个确定的值,而是一个来自于另一个查询的结果. 子 ...
随机推荐
- 使用IDEA创建一个Maven Web工程:无法创建Java Class文件
今天用IDEA新建了一个maven web工程,项目目录是这样的: 在新创建一个Java class 文件时,却没有Java class功能,无法创建,如图: 解决方案: 选择 File——>P ...
- 记一次SpringContextHolder.getBean出现异常NoClassDefFoundError: Could not initialize class
代码如下: public class TestUtils { private static UserDao logDao = SpringContextHolder.getBean(UserDao.c ...
- ssm 项目记录用户操作日志和异常日志
借助网上参考的内容,写出自己记录操作日志的心得!! 我用的是ssm项目使用aop记录日志:这里用到了aop的切点 和 自定义注解方式: 1.建好数据表: 数据库记录的字段有: 日志id .操作人.操作 ...
- JVM 类的卸载
1.当某个类被加载,连接和初始化后,它的生命周期就开始了.当代表这个类的Class对象不再被引用,即不可触及时,Class对象就会结束生命周期,这个类在方法区内的数据也会被卸载,从而结束这个类的生命周 ...
- Tosca 一不小心,我把那一排模块全关闭了,怎么打开
#写在前面, 之前用的时候,学了很多,基本都忘记了,现在再重新用,啥啥都不记得了,我还是应该事无巨细的全部记下来 红线这一排我关了好多,在哪儿打开 在这打开
- leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings
542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...
- lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II
变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...
- Xadmin权限管理
需求分析: 1.判断用户是否登陆,未登陆就不能进入其他页面2.为用户分配不同的权限,用户的操作只能在权限范围之内3.将用户可操作的权限显示在页面山,点击能进入该页面操作 模型表的建立 1.对每个用户建 ...
- 【转载】 linux系统dig和nslookup的安装
原文地址: https://blog.csdn.net/bjbs_270/article/details/7003088 --------------------------------------- ...
- python import注意事项
# 基本概念 ## 模块&包简介 模块:所谓模块就是一个.py文件,用来存放变量,方法的文件,便于在其他python文件中导入(通过import或from).包(package): 包是更大的 ...