MySQL的高级操作-一小部分

84 阅读2分钟

多表查询

mp.weixin.qq.com/s/QuiKoNZW5Dgc7XFmM8Qg

blog.csdn.net/weixin_4207…

多表查询是指一次查询涉及到多个表,可以使用三种不同类型的连接(联接)操作:内连接、左连接和右连接。

内连接:只返回两个表中共同匹配的行。

select * from1 inner join2 on1.id=表2.id;

左连接:返回左表中所有的行,以及右表中匹配的行。

右连接:返回右表中所有的行,以及左表中匹配的行。


 select * from1 right join2 on1.id=表2.id;

奇葩写法


SQL select * from Movies inner join Boxoffice where Movie_id=Id order by rating desc

image.png

  • 子查询---先欠着

SQL 函数

SQL 函数可以对数据进行处理和计算,常用的函数包括:

SQL聚合函数

select avg(*) from pdd where id=0;

  • avg(字段名):求平均值。

select count(字段名) from student where gender=1;

  • count(*):查看数据共多少条。

select max(字段名) from student where gender=1;

  • max(字段名):查看最大值。

select min(字段名) from student where gender=1;

  • min(字段名):查看最小值。

select sum(字段名) from student where gender=1;

  • sum(字段名):求每一列数值的和。

分组操作

分组要配合聚合函数一起使用

分组操作可以使用 group by 关键字实现,例如:


select name, count(*) from students group by name;

以上查询会统计每个学生名字出现的次数。

分组后的数据筛选---having

这个你需要注意下!!!

having

select name, count(*) from students group by name having name!='张三'

where 与 having 的区别:

注意:where 是对原始数据集的筛选,having 是对分组后的结果数据进行筛选。

注意:having 的运算符与 where 相同,像“逻辑运算符”、“比较运算符”

数据去重

数据不重复

就是有多条相同的数据上消除保留一条

select distinct(字段名) from 表名;

Python 的 Flask

Python 的 Flask 应用经常需要使用 SQL 数据库,因此以上的 SQL 高级应用对于 Flask 开发也非常有用。

说实话复制粘贴的时候自动给我sql加上了Plain Text真的很难受