Python MySQL数据库2:数据库查询(上)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: Python MySQL数据库2:数据库查询(上)

总体内容



  • 一、数据准备、基本的查询
  • 二、条件查询
  • 三、排序
  • 四、聚合、分组
  • 五、分页
  • 六、连接查询(多表的时候有用,单表的时候几乎没有用)
  • 七、自关联
  • 八、子查询
  • 九、总结


一、数据准备、基本的查询


  • 1.1、创建数据库 (pythonTestDataBase: 数据库名)


create database pythonTestDataBase charset=utf8;


image.png


  • 1.2、  使用数据库(pythonTestDataBase)


use pythonTestDataBase;
  • 1.3、创建两个数据表  animalTable 与 personTable


create table animalTable(
    id int unsigned primary key auto_increment not null,
    name varchar(20) default '',
    age tinyint unsigned default 0,
    gender enum('雄','雌','保密') default "保密",
    is_delete bit default 0
);
create table personTable(
    id int unsigned primary key auto_increment not null,
    name varchar(20) default '',
    gender enum('男','女','保密') default "保密",
    skinColor varchar(20) default '',
    is_delete bit default 0
);


image.png

  • 1.4、查看表结构


desc animalTable;
desc personTable;
  • 1.5、插入一些数据


insert into animalTable values(0, "梅花鹿", 3,"保密",0),(0, "熊猫", 2, "雌",0),(0, "东北虎", 6,"雄",0);
insert into personTable values(0, "小王","保密","黄",0),(0, "小李","男","黑",0),(0, "小杜","


image.png


1.6、基本的查询


  • (1)、查询所有字段


select * from 表名;
  • 例如:


select * from animalTable;
select * from personTable;
select id, name from animalTable;
  • (2)、查询指定字段


select 列1,列2,... from 表名;
  • 例如:


select name,gender from personTable;
  • (3)、使用 as 给字段起别名


select 字段 as 名字.... from 表名;
  • 例如:


select name as 姓名, gender as 性别 from personTable;


image.png

  • (4)、select 表名.字段 .... from 表名;


select 表名.name, 表名.age from 表名;
  • 例如:


select personTable.name, personTable.gender from personTable;
  • (5)、可以通过 as 给表起别名


select 别名.字段 .... from 表名 as 别名;
  • 例如


select s.name, s.gender from personTable as s;
  • (6)、消除重复行(distinct 字段)
select distinct gender from personTable;


二、条件查询



image.png

image.png

  • 2.1、比较运算符(使用 animalTable 表)


  • (1)、>


// 查看大于 6 岁的动物
select * from animalTable where age>6;
select id,name,gender from animalTable where age>6;
  • (2)、<


// 查看小于 6 岁的动物
select * from animalTable where age<6;
  • (3)、=>=<=


// 查看 等于、等于等于、小于等于 6 岁的动物
select * from animalTable where age=6;
select * from animalTable where age>=6;
select * from animalTable where age<=6;
  • (4)、!=


// 查看不等于 6 岁的动物
select * from animalTable where age!=6;
  • 2.2、逻辑运算符(使用 animalTable 表)
  • (1)、and


// 3到12之间的动物信息
select * from animalTable where age>3 and age<12;


image.png

  • (2)、or


// 6以上或者性别是保密的
select * from animalTable where age>6 or gender="保密";
  • (3)、not


// 不在 7岁以上的雄性 这个范围内的信息 
select * from animalTable where not (age>7 and gender = "雄");


image.png

// 年龄不是小于或者等于7 并且是雄性
select * from animalTable where  (not age <=7) and gender = "雄";


image.png

2.3、模糊查询(使用 personTable 表),效率比较低


image.png


(1)、like% 替换1个或者多个、_ 替换1个、查询姓名中 以 "关键字名" 开始的名字


  • % 替换1个或者多个


// 查询姓名中 有 "杰" 所有的名字
select * from personTable where name like "%杰%";


image.png


  • _ 替换1个


// 查询姓名中 有姓 "周" 所有2个字的名字
select * from personTable where name like "周_";
// 查询姓名中 有姓 "周" 所有3个字的名字
select * from personTable where name like "周__";
// 查询至少有2个字的名字
select name from personTable where name like "__%";
  • 查询姓名中 以 "关键字名" 开始的名字


// 查询姓名中 以 "小" 开始的名字
select name from personTable where name like "小%";
  • (2)、rlike 正则


// 查询以 周开始的姓名
select * from personTable where name rlike "^周.*";
// 查询以 周开始、伦结尾的姓名
select name from personTable where name rlike "^周.*伦$";


image.png

2.4、范围查询、null(使用 personTable 表)

image.png

in (1, 3, 8)表示在一个非连续的范围内

// 查询 身高为为172、178 的姓名
select * from personTable where height in (172, 178);


image.png

  • not in 不 非连续 的范围之内


// 查询 不是 身高为为172、178 的姓名
select * from personTable where height not in (172, 178);
  • between ... and ...表示在一个连续的范围内


查询 身高在 在172到180之间的的信息
select * from personTable where height betwen 172 in 180;


image.png

  • not between ... and ... 表示 不在一个连续的范围内


查询 身高在 不 在172到180之间的的信息
select * from personTable where height not betwen 172 in 180;
或者 (使用上面的即可)
select * from personTable where not height betwen 172 in 180;
  • 空(null)判断: 判空is null
    理解一个概念: name = null  与 name = "" 的区别,前者是 name没有指向任何地址,后者是指向一个空的地址


// 查询身高为 空(null) 的信息
select * from personTable where height is null;

image.png


  • 判非空 is not null


// 查询身高 不为 空(null) 的信息
 select * from personTable where height is not null;


三、排序 order by 字段,使用 animalTable 表



image.png


  • 3.1、order by` 字段
  • asc小到大 排列,即 升序
  • desc大到小 排序,即 降序
  • 3.2、默认是 升序(第2句与第3句一个意思)


// 查询年龄在3到12岁之间的雄性动物(默认按照 id 排序 )
select * from animalTable where (age between 3 and 12) and gender = "雄";
查询年龄在3到12岁之间的雄性动物,按照年龄从小到大排序
select * from animalTable where (age between 3 and 12) and gender = "雄" order by age;
select * from animalTable where (age between 3 and 12) and gender = "雄" order by age asc;


image.png


3.3、order by 多个字段

查询年龄在3到12岁之间的雄性动物,按照年龄从小到大排序,如果年龄相同的情况下按照 id 从大到小排序( id 默认是从小到大的)

select * from animalTable where (age between 3 and 12) and gender = "雄" order by age asc,i


image.png


3.4、不需要 where 约束

按照年龄从小到大、id 从大到小的排序

select * from animalTable order by age asc,id desc;


image.png


四、聚合、分组



image.png


4.1、聚合


  • (1)、count 计算个数
    查询雄性有多少人


select count(*) as 雄性数量 from animalTable where gender = "雄";


image.png

  • 查询雌性有多少


select count(*) as 女性人数 from personTable where gender = "女";
  • (2)、最大值  max  与 最小值 min
    查询最大的年龄


select max(age) from animalTable;


image.png


  • (3)、求和  sum
    求年龄的和


select sum(age) from animalTable;
  • (4)、平均值  avg
    求年龄的平均值


select avg(age) from animalTable;
  • 还可以用  sum(age)/count(*) 来计算平均年龄


select sum(age)/count(*) from animalTable;
  • (5)、四舍五入 round(值 , 保留小数的位数)  小数
    计算所有动物的平均年龄,保留2位小数


select round(avg(age),2) from animalTable;


image.png


  • 计算雄性的平均年龄 保留2位小数


select round(avg(age),2) from animalTable where gender = "雄";
  • 4.2、分组group by


  • group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组
  • group by可用于单个字段分组,也可用于多个字段分组
  • (1)、按照 性别 分组,查询所有的性别
    查询所有性别的动物组


select gender from animalTable group by gender;
  • (2)、计算每种 性别 中的人数


select gender,count(*) from animalTable group by gender;


image.png

(3)、计算雄性的数量(取出分组中 雄 性的组),根据条件取出分组中的某一个分组

select gender,count(*) from animalTable where gender = "雄" group by gender;


image.png


(4)、group by + group_concat()  显示分组中的 指定字段


显示分组中的 name

  • group_concat(字段名)可以作为一个输出字段来使用,
  • 表示分组之后,根据分组结果,使用group_concat()来放置每一组的某字段的值的集合

select gender,group_concat(name) from animalTable where gender="雄" group by gender;


image.png


相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
3天前
|
存储 关系型数据库 MySQL
mysql数据库查询时用到的分页方法有哪些
【8月更文挑战第16天】在MySQL中,实现分页的主要方法包括:1)使用`LIMIT`子句,简单直接但随页数增加性能下降;2)通过子查询优化`LIMIT`分页,提高大页码时的查询效率;3)利用存储过程封装分页逻辑,便于复用但需额外维护;4)借助MySQL变量实现,可能提供更好的性能但实现较复杂。这些方法各有优缺点,可根据实际需求选择适用方案。
|
1天前
|
SQL 存储 关系型数据库
数据库-MySQL-01(一)
数据库-MySQL-01(一)
13 4
|
3天前
|
SQL NoSQL 数据库
在Python中使用sqlalchemy来操作数据库的几个小总结
在探索使用 FastAPI, SQLAlchemy, Pydantic,Redis, JWT 构建的项目的时候,其中数据库访问采用SQLAlchemy,并采用异步方式。数据库操作和控制器操作,采用基类继承的方式减少重复代码,提高代码复用性。在这个过程中设计接口和测试的时候,对一些问题进行跟踪解决,并记录供参考。
|
1天前
|
存储 关系型数据库 MySQL
MySQL bit类型增加索引后查询结果不正确案例浅析
【8月更文挑战第17天】在MySQL中,`BIT`类型字段在添加索引后可能出现查询结果异常。表现为查询结果与预期不符,如返回错误记录或遗漏部分数据。原因包括索引使用不当、数据存储及比较问题,以及索引创建时未充分考虑`BIT`特性。解决方法涉及正确运用索引、理解`BIT`的存储和比较机制,以及合理创建索引以覆盖各种查询条件。通过`EXPLAIN`分析执行计划可帮助诊断和优化查询。
|
5天前
|
关系型数据库 MySQL 索引
mysql8.0中fulltext不能查询到中文的解决方法
确保MySQL服务器字符集为`utf8mb4`,并设置`ngram_token_size=1`以支持单字搜索。如已更改此参数且存在全文索引,需删除原有索引并重建,使用`WITH PARSER ngram`指定解析器。例如: ``` ALTER TABLE your_table DROP INDEX idx_fulltext, ADD FULLTEXT INDEX idx_fulltext (your_column) WITH PARSER ngram; ```
|
1天前
|
SQL 存储 关系型数据库
数据库-MySQL-03
数据库-MySQL-03
8 0
|
1天前
|
SQL 数据库
数据库-MySQL-02(二)
数据库-MySQL-02(二)
4 0
|
1天前
|
SQL 关系型数据库 MySQL
数据库-MySQL-02(一)
数据库-MySQL-02(一)
8 0
|
1天前
|
SQL 存储 数据库
数据库-MySQL-01(二)
数据库-MySQL-01(二)
5 0
|
4天前
|
存储 SQL 关系型数据库
探索MySQL的执行奥秘:从查询执行到数据存储与优化的深入解析
探索MySQL的执行奥秘:从查询执行到数据存储与优化的深入解析