python操作mysql(四)
1. 删除数据
mysql> select course.cid, course.tid, score.grade, teacher.tname, count(teacher.tid) as count_teacher from course join score on score.grade < 60 and course.cid=score.cid join teacher on course.tid=teacher.tid group by course.tid order by count_teacher desc limit 5;
+-----+-----+-------+--------------------------+---------------+
| cid | tid | grade | tname | count_teacher |
+-----+-----+-------+--------------------------+---------------+
| 12 | 127 | 45 | 574c3344f37b9c6f3e | 587 |
| 14 | 190 | 21 | ab5f646915d0005657dd7074 | 560 |
| 16 | 112 | 40 | e270e527 | 555 |
| 11 | 157 | 19 | 86703bbdf4f546fb | 550 |
| 13 | 186 | 12 | 827be3f52efd52596563cc | 546 |
+-----+-----+-------+--------------------------+---------------+
5 rows in set (0.00 sec)mysql> select tid from (select course.cid, course.tid, score.grade, teacher.tname, count(teacher.tid) as count_teacher from course join score on score.grade < 60 and course.cid=score.cid join teacher on course.tid=teacher.tid group by course.tid order by count_teacher desc limit 5) as delete_teacher_id; //必须重命名得到的字段,否则报错ERROR 1248 (42000): Every derived table must have its own alias
+-----+
| tid |
+-----+
| 127 |
| 190 |
| 112 |
| 157 |
| 186 |
+-----+
5 rows in set (0.00 sec)mysql> delete from teacher where tid in (select tid from (select course.cid, course.tid, score.grade, teacher.tname, count(teacher.tid) as count_teacher from course join score on score.grade < 60 and course.cid=score.cid join teacher on course.tid=teacher.tid group by course.tid order by count_teacher desc limit 5) as delete_teacher_id);
Query OK, 5 rows affected (0.01 sec)mysql> select course.cid, course.tid, score.grade, teacher.tname, count(teacher.tid) as count_teacher from course join score on score.grade < 60 and course.cid=score.cid join teacher on course.tid=teacher.tid group by course.tid order by count_teacher desc limit 5;
+-----+-----+-------+-------------------------+---------------+
| cid | tid | grade | tname | count_teacher |
+-----+-----+-------+-------------------------+---------------+
| 19 | 105 | 15 | 89c19462c2a967 | 524 |
| 20 | 101 | 18 | b78aa7d6a3f40960a062dad | 520 |
| 18 | 118 | 22 | 3ab1117efe9f4aa | 518 |
| 15 | 167 | 52 | abf2c6018fc358766 | 512 |
| 17 | 116 | 46 | ff27c156ca5b8b47cb76 | 509 |
+-----+-----+-------+-------------------------+---------------+
5 rows in set (0.01 sec)
- 查询course表的课程id 和 老师id
- 关联score表,查询课程表的课程id和分数表的课程id 对应的少于60分的分数, 这时候得到的是所有不及格的课程
- 关联teacher表,查询课程表的老师id和老师表的老师id 对应的老师名字和老师id, 这时候得到的是这个老师负责的所有不及格的课程
- 把课程表的课程id分组,对组里的老师id做统计,然后排序得出前5
- 从teacher表中删除这5个tid对应的老师
2. 更新数据
mysql> select *, (grade+60) as new_grade from score where grade <5 limit 10;
+-------+-------+-----+-------+-----------+
| sid | stdid | cid | grade | new_grade |
+-------+-------+-----+-------+-----------+
| 10003 | 13579 | 11 | 2 | 62 |
| 10034 | 11737 | 18 | 2 | 62 |
| 10061 | 14361 | 15 | 4 | 64 |
| 10089 | 19056 | 18 | 1 | 61 |
| 10103 | 15362 | 18 | 4 | 64 |
| 10109 | 17304 | 13 | 3 | 63 |
| 10127 | 10554 | 13 | 2 | 62 |
| 10185 | 16417 | 20 | 1 | 61 |
| 10199 | 15302 | 16 | 4 | 64 |
| 10225 | 19942 | 18 | 1 | 61 |
+-------+-------+-----+-------+-----------+
10 rows in set (0.00 sec)
更新数据:把低于5分的都加60分
mysql> update score set grade=grade+60 where grade < 5; Query OK, 434 rows affected (0.00 sec) Rows matched: 434 Changed: 434 Warnings: 0mysql> select * from score where grade <5; Empty set (0.00 sec)
3. mysql索引
- 索引的概念
- 索引的分类
- 2.1 普通索引
- 2.2 唯一索引
- 2.3 主索引
- 2.4 外键索引
- 2.5 复合索引
- mysql主键和索引的区别
mysql> alter table score add index idx_stdid_cid(stdid, cid); //为stdid字段和cid字段创建一个复合索引 Query OK, 0 rows affected (0.16 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> explain select * from score where stdid=17654; //使用explain关键字查看查询是否使用了索引 +----+-------------+-------+------+---------------+---------------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+---------------+---------+-------+------+-------+ | 1 | SIMPLE | score | ref | idx_stdid_cid | idx_stdid_cid | 4 | const | 2 | NULL | +----+-------------+-------+------+---------------+---------------+---------+-------+------+-------+ 1 row in set (0.00 sec)mysql> select * from score where stdid=17654; +-------+-------+-----+-------+ | sid | stdid | cid | grade | +-------+-------+-----+-------+ | 10760 | 17654 | 12 | 35 | | 10005 | 17654 | 13 | 12 | +-------+-------+-----+-------+ 2 rows in set (0.00 sec)mysql> explain select * from score where stdid=10380 and cid=13; //idx_stdid_cid(stdid, cid)这个复合索引只能用作字段stdid的索引或者stdid和cid共同查询时使用的索引 +----+-------------+-------+------+---------------+---------------+---------+-------------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+---------------+---------+-------------+------+-------+ | 1 | SIMPLE | score | ref | idx_stdid_cid | idx_stdid_cid | 8 | const,const | 1 | NULL | +----+-------------+-------+------+---------------+---------------+---------+-------------+------+-------+ 1 row in set (0.01 sec)mysql> select * from score where stdid=10380 and cid=13; +-------+-------+-----+-------+ | sid | stdid | cid | grade | +-------+-------+-----+-------+ | 10007 | 10380 | 13 | 36 | +-------+-------+-----+-------+ 1 row in set (0.00 sec)mysql> explain select * from score where stdid=10380 and cid>13; +----+-------------+-------+-------+---------------+---------------+---------+------+------+-----------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+---------------+---------+------+------+-----------------------+ | 1 | SIMPLE | score | range | idx_stdid_cid | idx_stdid_cid | 8 | NULL | 2 | Using index condition | +----+-------------+-------+-------+---------------+---------------+---------+------+------+-----------------------+ 1 row in set (0.00 sec)mysql> explain select * from score where stdid>10380 and cid>13; //如果where条件没有使用关系型运算符= , 那么查询不走索引 +----+-------------+-------+------+---------------+------+---------+------+-------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+-------+-------------+ | 1 | SIMPLE | score | ALL | idx_stdid_cid | NULL | NULL | NULL | 10294 | Using where | +----+-------------+-------+------+---------------+------+---------+------+-------+-------------+ 1 row in set (0.00 sec)
转载于:https://www.cnblogs.com/tobeone/p/7888923.html
python操作mysql(四)相关推荐
- python mysql倒序_day40:MySQL:python操作mysql:pymysql模块SQL注入攻击
目录 part1:用python连接mysql 1.用python连接mysql的基本语法 创建连接conn→创建游标对象cursor→执行sql语句execute→获取数据fetchone→释放游标 ...
- python操作MySQL 模拟简单银行转账操作
一.基础知识 1.MySQL-python的安装 下载,然后 pip install 安装包 2.python编写通用数据库程序的API规范 (1).数据库连接对象 connection,建立pyth ...
- python操作mysql中的表_带你了解什么是Python操作MySQL数据库
写这篇文章主要是为了介绍Python操作MySQL数据库,并结合相应的实例带你更加深入了解.文中的代码实例很详细,对大家有一定的参考学习价值. 1.什么是pymysql? PyMySQL是在Pytho ...
- python操作mysql事务提交_python关于Mysql操作
一.安装mysql windows下,直接下载mysql安装文件,双击安装文件下一步进行操作即可, Linux下的安装也很简单,除了下载安装包进行安装外,一般的linux仓库中都会有mysql ,我们 ...
- Python操作MySQL
一 用户管理 用户在数据库操作系统中,是一个设计到数据安全的非常重要的数据库对象,在MySQL系统的数据库中root用户作为超级管理员用户,通常情况下不会给普通用户使用,所以掌握数据库管理系统中的用户 ...
- python操作mysql数据库练习
python操作mysql数据库练习 本次练习是在windows下,mysql版本为5.7,python版本为2.7.5,集成环境为pycharm. 创建表时,enign在innodb下支持事务,其他 ...
- Python操作MySQL分享
Python操作MySQL Python操作MySQL 关于MySQL数据库 开始之前 _mysql模块 MySQLdb模块 1. 第一个例子 2. 创建并填充表 3. 取回数据 4. 字典游标 5. ...
- 数据库——python操作MySQL、修改表SQL语句、视图、触发器、存储过程、事务、流程控制、常见函数、索引
文章目录 一.python操作MySQL 1.pymysql模块 1.基本使用 2.sql注入问题 3.二次确认 二.修改表SQL语句 1.修改表的名字 rename 2.添加字段 add 3.修改字 ...
- python socket mysql_5.Python操作MySQL,三层架构,Socket网络编程
Python操作MySQL - MySQL之查询操作 - MySQL之插入数据 - MySQL之批量插入数据 - MySQL之删除数据 - MySQL之更新数据库 - MySQL之事务 - MySQL ...
- 15.python操作mysql
15.python操作mysql 导入包 from pymysql import* 1. 创建 Connection 连接 conn=conne(host='192.168.13.130',port= ...
最新文章
- Nginx配置和内核优化 实现突破十万并发
- App用户体验的一点思考
- MySql命令行基本操作
- 暑期训练日志----2018.8.16
- Server 2016DC查看五种AD角色的方法
- 拓端tecdat|matlab使用样条插值重采样估计INR数据研究
- springboot 配置mybatis
- Python 科学计算
- 刷网课-踩坑——jsDOM获取不到页面元素节点-iframe标签获取元素节点
- http://bt.neu6.edu.cn/forum.php,分享一些教育网访问较快的站点~
- 今天把中国建设银行APP4.2.1版iOS客户端里所有的功能都点了一遍
- 知识图谱构建技术简介
- HDU5285.wyh2000 and pupil
- postgres使用with计算占比,同比和环比
- Python串口操作纸币器1
- 静态链接库(Lib) 与 动态链接库(DLL)
- 医学图像分割知识点总结
- 微信小程序影视评论交流平台系统毕业设计毕设(2)小程序功能
- 聊聊APP数据分析的那些思路
- unity音乐计算机谱子,Unity钢琴简谱-数字双手-Alan Walker
热门文章
- java和python哪个好就业2020-python和java自学哪个好?
- 用python绘制漂亮的图形-用Python画一些漂亮图形--Quora代码赏析
- python处理excel-使用python将数据写入excel
- python中文意思k-python中“//”表示什么意思_后端开发
- python电脑下载什么软件好-Python工具 | 4个好用的开源 Python 下载器
- python游戏脚本实例-python实现的简单文本类游戏实例
- 如何自学python数据分析-Python学习干货 |如何用Python进行数据分析?
- 学python就业要看哪些书-编程0基础自学Python,踩完这些坑,我才成功就业!
- 学python那本书比较好-Python初学者最应该读的8本书
- python while循环语句-Python