3Python全栈之路系列之MySQL表内操作

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:

Python全栈之路系列之My

1
<br>

SQL表内操作


先创创建一个表用于测试

1
2
3
4
5
6
7
8
9
10
11
12
- -  创建数据库
CREATE DATABASE dbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
- -  创建表
CREATE TABLE `tb` (
   ` id int ( 5 ) NOT NULL AUTO_INCREMENT,
   `name` char( 15 ) NOT NULL,
   `alias` varchar( 10 ) DEFAULT NULL,
   `email` varchar( 30 ) DEFAULT NULL,
   `password` varchar( 20 ) NOT NULL,
   `phone` char( 11 ) DEFAULT  '13800138000' ,
   PRIMARY KEY (` id `,`name`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8;

增加表内数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  进入dbname数据库
mysql> use dbname
Database changed
# 查看当前库所有的表
mysql> show tables;
+ - - - - - - - - - - - - - - - - - - +
| Tables_in_dbname |
+ - - - - - - - - - - - - - - - - - - +
| tb               |
+ - - - - - - - - - - - - - - - - - - +
1  row  in  set  ( 0.00  sec)
# 查看tb表内的内容
mysql> select  *  from  tb;
Empty  set  ( 0.00  sec)
1
2
3
4
- -  插入单条数据
insert into tb(name,email,password) values( "ansheng" , "anshengme.com@gmail.com" , "as" );
- -  同时插入多条数据
insert into tb(name,email,password) values( "as" , "i@anshengme.com" , "pwd" ),( "info" , "info@anshengme.com" , "i" );

查看插入的数据

1
2
3
4
5
6
7
8
9
mysql> select  *  from  tb;
+ - - - - + - - - - - - - - - + - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - + - - - - - - - - - - - - - +
id  | name    | alias | email                   | password | phone       |
+ - - - - + - - - - - - - - - + - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - + - - - - - - - - - - - - - +
|   2  | ansheng | NULL  | anshengme.com@gmail.com | as       |  13800138000  |
|   3  | as      | NULL  | i@anshengme.com         | pwd      |  13800138000  |
|   4  | info    | NULL  | info@anshengme.com      | i        |  13800138000  |
+ - - - - + - - - - - - - - - + - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - + - - - - - - - - - - - - - +
3  rows  in  set  ( 0.00  sec)

把别的表的数据插入当前表

查看tb_copy表内的内容

1
2
3
4
5
6
7
8
9
mysql> select  *  from  tb_copy;
+ - - - - + - - - - - - - - + - - - - - - - + - - - - - - - + - - - - - - - - - - + - - - - - - - - - - - - - +
id  | name   | alias | email | password | phone       |
+ - - - - + - - - - - - - - + - - - - - - - + - - - - - - - + - - - - - - - - - - + - - - - - - - - - - - - - +
|   5  | hello  | NULL  | NULL  |  1         13800138000  |
|   6  | word   | NULL  | NULL  |  2         13800138000  |
|   7  | python | NULL  | NULL  |  3         13800138000  |
+ - - - - + - - - - - - - - + - - - - - - - + - - - - - - - + - - - - - - - - - - + - - - - - - - - - - - - - +
3  rows  in  set  ( 0.00  sec)

把tb_copy表内的name,email,password列插入到tb表中

1
insert into tb (name, email, password) select name,email,password  from  tb_copy;

查询tb内的内容

1
2
3
4
5
6
7
8
9
10
11
12
mysql> select  *  from  tb;
+ - - - - + - - - - - - - - - + - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - + - - - - - - - - - - - - - +
id  | name    | alias | email                   | password | phone       |
+ - - - - + - - - - - - - - - + - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - + - - - - - - - - - - - - - +
|   2  | ansheng | NULL  | anshengme.com@gmail.com | as       |  13800138000  |
|   3  | as      | NULL  | i@anshengme.com         | pwd      |  13800138000  |
|   4  | info    | NULL  | info@anshengme.com      | i        |  13800138000  |
|   5  | hello   | NULL  | NULL                    |  1         13800138000  |
|   6  | word    | NULL  | NULL                    |  2         13800138000  |
|   7  | python  | NULL  | NULL                    |  3         13800138000  |
+ - - - - + - - - - - - - - - + - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - + - - - - - - - - - - - - - +
6  rows  in  set  ( 0.00  sec)

删除表内数据

1
2
- -  删除表内的所有内容
delete  from  tb_copy;
1
2
- -  删除表内某一条数据
delete  from  tb where  id = 2  and  name = "ansheng" ;

更改表内数据

1
update tb  set  name = "as"  where  id = "3" ;

1
2
3
4
- -  查询表内所有内容
select  *  from  tb;
- -  带条件的查询表内的内容
select  *  from  tb where  id  4 ;

查询的时候指定最后一列的名称

1
2
3
4
5
6
7
8
9
mysql> select  id ,name as username  from  tb where  id  4 ;
+ - - - - + - - - - - - - - - - +
id  | username |
+ - - - - + - - - - - - - - - - +
|   5  | hello    |
|   6  | word     |
|   7  | python   |
+ - - - - + - - - - - - - - - - +
3  rows  in  set  ( 0.00  sec)

其他操作

条件

1
2
3
4
5
6
7
8
9
10
- -  多条件查询
select  *  from  tb where  id > 3  and  name = "hello"  and  password = "1" ;
- -  查询指定范围
select  *  from  tb where  id  between  4  and  6 ;
- -  查询括号内存在的数据
select  *  from  tb where  id  in  ( 4 , 6 );
- -  查询括号内不存在的数据
select  *  from  tb where  id  not  in  ( 4 , 6 );
- -  以别的表的内容为查询条件
select  *  from  tb where  id  in  (select  id  from  tb_copy);

通配符

1
2
3
4
- -  以p开头的所有(多个字符串)
select  *  from  tb where name like  "p%" ;
- -  以p开头的所有(一个字符)
select  *  from  tb where name like  "p%" ;

限制

1
2
3
4
5
6
- -  前三行数据
select  *  from  tb limit  3 ;
- -  从第 2 行开始的 3
select  *  from  tb limit  2 , 3 ;
- -  从第 4 行开始的 5
select  *  from  tb limit  5  offset  4 ;

排序

1
2
3
4
5
6
- -  根据 "name" 列从小到大排列
select  *  from  tb order by name asc;
- -  根据 "name" 列从大到小排列
select  *  from  tb order by name desc;
- -  根据 “列 1 ” 从大到小排列,如果相同则按列 2 从小到大排序
select  *  from  表 order by 列 1  desc,列 2  asc;

分组

1
2
3
4
5
select  id  from  tb group by  id ;
select  id ,name  from  tb group by  id ,name;
select num,nid  from  表  where nid >  10  group by num,nid order nid desc;
select num,nid,count( * ), sum (score), max (score), min (score)  from  表 group by num,nid;
select num  from  表 group by num having  max ( id ) >  10 ;

特别的:group by 必须在where之后,order by之前

连表

无对应关系则不显示

1
select A.num, A.name, B.name  from  A,B where A.nid  =  B.nid;

无对应关系则不显示

1
select A.num, A.name, B.name  from  A inner join B on A.nid  =  B.nid;

A表所有显示,如果B中无对应关系,则值为null

1
select A.num, A.name, B.name  from  A left join B on A.nid  =  B.nid;

B表所有显示,如果B中无对应关系,则值为null

1
select A.num, A.name, B.name  from  A right join B on A.nid  =  B.nid;

组合

组合,自动处理重合

1
select nickname  from  A union select name  from  B;

组合,不处理重合

1
select nickname  from  A union  all  select name  from  B;

#Python全栈之路











本文转自 Edenwy  51CTO博客,原文链接:http://blog.51cto.com/edeny/1925870,如需转载请自行联系原作者
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
打赏
0
0
0
0
235
分享
相关文章
Python中使用MySQL模糊查询的方法
本文介绍了两种使用Python进行MySQL模糊查询的方法:一是使用`pymysql`库,二是使用`mysql-connector-python`库。通过这两种方法,可以连接MySQL数据库并执行模糊查询。具体步骤包括安装库、配置数据库连接参数、编写SQL查询语句以及处理查询结果。文中详细展示了代码示例,并提供了注意事项,如替换数据库连接信息、正确使用通配符和关闭数据库连接等。确保在实际应用中注意SQL注入风险,使用参数化查询以保障安全性。
Python处理数据库:MySQL与SQLite详解 | python小知识
本文详细介绍了如何使用Python操作MySQL和SQLite数据库,包括安装必要的库、连接数据库、执行增删改查等基本操作,适合初学者快速上手。
535 15
Python Web开发:Django框架下的全栈开发实战
【10月更文挑战第27天】本文介绍了Django框架在Python Web开发中的应用,涵盖了Django与Flask等框架的比较、项目结构、模型、视图、模板和URL配置等内容,并展示了实际代码示例,帮助读者快速掌握Django全栈开发的核心技术。
389 45
Python Web开发:Django框架下的全栈开发实战
【10月更文挑战第26天】本文详细介绍了如何在Django框架下进行全栈开发,包括环境安装与配置、创建项目和应用、定义模型类、运行数据库迁移、创建视图和URL映射、编写模板以及启动开发服务器等步骤,并通过示例代码展示了具体实现过程。
176 2
Mysql学习笔记(四):Python与Mysql交互--实现增删改查
如何使用Python与MySQL数据库进行交互,实现增删改查等基本操作的教程。
108 1
pymysql:Python操作MySQL数据库的又一利器
pymysql:Python操作MySQL数据库的又一利器
69 0
Python全栈 Web(Ajax JQuery-AJAX 跨域请求)
Flask、Python、Django、框架、服务器、客户端、浏览器、交互、WEB、Python前端、CSS、JAVA、HTML、H5、PHP、JavaScript、JQuery、分布式开发
5250 0
Python全栈 Web(Ajax JSON JQuery)
Flask、Python、Django、框架、服务器、客户端、浏览器、交互、WEB、Python前端、CSS、JAVA、HTML、H5、PHP、JavaScript、JQuery、分布式开发
3013 0
Python全栈 Web(jQuery 一条龙服务)
jQuery是一个轻量级的JS库 - 是一个被封装好的JS文件,提供了更为简便的元素操作方式
3362 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等