-- 查询
-- 查询所有字段
-- select * from 表名;
select * from students;
-- 查询指定字段
-- select 列1,列2,... from 表名;
select name,age from students;
-- 使用 as 给字段起别名
-- select 字段 as 名字.... from 表名;
select name as '姓名',age as '年龄' from students; -- as 可以省略
select name '姓名',age '年龄' from students;
-- sql语句完全的形式
select students.* from students;
select python_test_1.students.* from students;
-- 如果查询的表就在当前使用的数据库中存在 那么数据库名可以省略
-- 当一个sql语句中只有一个数据表的时候 数据表名可以省略
-- select 表名.字段 .... from 表名;
select students.name,students.age from students;
-- 可以通过 as 给表起别名
-- select 别名.字段 .... from 表名 as 别名;
select s.name, s.age from students as s; -- 在这个sql语句中不能够再使用students;
-- 消除重复行
-- distinct 字段
-- 查询班级学生的性别
select gender from students;
-- 查询班级有多少种性别
select distinct gender from students;
select distinct id,gender from students;
-- 条件查询
select * from students where 1 > 0;
-- 比较运算符
-- >
-- 查询大于18岁的信息
select * from students where age > 18;
-- <
-- 查询小于18岁的信息
select * from students where age < 18;
-- >=
-- <=
-- 查询小于或者等于18岁的信息
select * from students where age <= 18;
-- = 而不是 '=='
-- 查询年龄为18岁的所有学生的名字
select * from students where age = 18;
-- != 或者 <> 实际开发中 最好使用 ! 表示不等于
select * from students where age != 18;
-- <> 不够通用
-- 逻辑运算符
-- and 且
-- 18岁以上的女性
select * from students where age > 18 and gender = 2;
-- or 或
-- 18以上或者身高超过180(包含)以上
select * from students where age > 18 or height >= 180;
-- not 非
-- 年龄不是18岁的学生
select * from students where age != 18;
select * from students where not age = 18;
-- 年龄是小于或者等于18 并且是女性
select * from students where age <= 18 and gender = 2;
-- 模糊查询
-- like
-- % 表示任意字符可有可无
-- 查询姓名中 以 "小" 开始的名字
select * from students where name like "小%";
select * from students where name like "%杰%";
-- _ 表示任意一个字符
-- 查询有2个字的名字
select * from students where name like "__";
-- 查询有3个字的名字
select * from students where name like "___";
-- rlike 正则
-- match findall re模块给你封装的方法
-- r"[a-z]{6}"
-- 查询以 周开始的姓名
select * from students where name rlike "^周.*";
select * from students where name rlike "^周.*伦$";
-- 范围查询
-- in表示在一个非连续的范围内
-- 查询 年龄为18、34岁的学生
select * from students where age = 18 or age = 34;
select * from students where age in (18, 34);
-- not in 不在非连续的范围之内
-- 年龄不是 18、34岁的学生的信息
select * from students where age not in (18, 34);
-- 年龄不是 18、34岁之间的信息
select * from students where age < 18 or age > 34;
-- 18 ~ 34
select * from students where not (age < 18 or age > 34);
-- between ... and ...表示在一个连续的范围内 两边都会包含
-- 查询 年龄在18到34之间的的信息
select * from students where age between 18 and 34;
-- not between ... and ...表示不在一个连续的范围内
-- 查询 年龄不在在18到34之间的的信息
select * from students where age not between 18 and 34;
select * from students where not age between 18 and 34;
-- 空判断 null 不能够使用比较运算符
-- 查询身高为空的信息
# 错误select * from students where height = null;
select * from students where height is null;
-- 查询身高不为空的学生
select * from students where height is not null;
-- 排序 从大到小--> 降序排序 从小到大-->升序排序
-- order by 字段 默认就是升序排序 asc 可以省略
-- asc从小到大排列,即升序
-- 查询年龄在18到34岁之间的男性,按照年龄从小到大排序
select * from students where age between 18 and 34 and gender = 1 order by age asc;
-- 降序 desc
-- desc从大到小排序,即降序
-- 查询年龄在18到34岁之间的女性,身高从高到矮排序
select * from students where age between 18 and 34 and gender = 2 order by height desc;
-- order by 多个字段 order by age asc, height desc
-- 查询年龄在18到34岁之间的女性,身高从高到矮排序, 如果身高相同的情况下按照年龄从小到大排序
select * from students where age between 18 and 34 and gender = 2 order by height desc,age asc;
-- 查询年龄在18到34岁之间的女性,身高从高到矮排序, 如果身高相同的情况下按照年龄从小到大排序, 如果年龄也相同那么按照id从大到小排序
select * from students where age between 18 and 34 and gender = 2 order by height desc,age asc,id desc;
-- 按照年龄从小到大、身高从高到矮的排序
select * from students order by age asc, height desc;
排序规则默认就是升序排序, asc 可以省略,但是不推荐省略
-- 聚合函数 --> 为了统计而生
-- 总数
-- 统计班级的总人数
select count(*) from students;
select count(id) from students;
-- count(*) 以行单位来进行统计个数
-- count(*) 效率更高, 效率略差:count(id)--> 获取对应的行--> 获取该行对应字段是否为NULL
-- 查询男性有多少人,女性有多少人
select count(*) from students where gender = 1;
select count(*) from students where gender = 2;
-- 最大值
-- max()
-- 查询最大的年龄
select max(age) from students;
-- 查询女性的最高 身高
select max(height) from students where gender = 2;
-- 查询最大年龄的学生的名字
# select max(age),name from students where gender = 2;
select name from students where age = (select max(age) from students);
# 错误: select name, max(height) from students;
-- 最小值
-- min()
-- 求和
-- sum()
-- 计算所有人的年龄总和
select sum(age) from students;
-- 计算平均年龄
select sum(age) / count(*) from students;
# 错误select sum(height) / count(*) from students;
select sum(height) / count(height) from students;
-- 平均值
-- avg()
-- 计算平均年龄
select avg(age) from students;
-- 计算平均身高
select sum(height) / count(height) from students;
-- 四舍五入 round(123.23 , 1) 保留1位小数
-- 计算所有人的平均年龄,保留2位小数
select round(avg(age),2) from students;
-- 计算男性的平均身高 保留2位小数
-- 分组
-- group by 字段
-- 查询班级学生的性别
select gender from students;
-- 查看有哪几种性别
select distinct gender from students;
-- 按照性别分组
select gender from students group by gender;
-- 计算每种性别中的人数
select gender,count(*) from students group by gender;
-- 获取每个性别下对应的学生的名字
正确: select gender, group_concat(name) from students group by gender;
错误: select gender,name from students group by gender;
错误: select gender, * from students group by gender;
男 刘德华 彭于晏, ..
女 静香, 周杰, ..
中性 金星
保密 凤姐
-- 查询同种性别中的姓名和身高
select gender,group_concat(name,height) from students group by gender;
-- 计算男性的人数
select count(*) from students where gender = 1;
-- 通过分组来实现
select gender, count(*) from students group by gender having gender = 1;
-- 分组之后的数据进行条件筛选 需要使用having 而不是使用where
-- 使用having
-- 可以使用having 表示对于已经分组的数据做进一步的筛选
-- 除了男生以外的分组的人数
select gender, count(*) from students group by gender having gender != 1;
select gender, count(*) from students group by gender having not gender = 1;
# 错误select gender, count(*) from students group by gender not having gender = 1;
-- having 对于分组之后的数据 做进一步的筛选
-- 查询每种性别中的平均年龄avg(age)
select gender, avg(age) from students group by gender;
-- 查询每种性别中的平均年龄avg(age), 最大年龄,平均身高,最高身高
select gender, avg(age),max(age), avg(height), max(height) from students group by gender;
-- 查询平均年龄超过30岁的性别,以及姓名 性别的平均年龄超过30岁
select gender,group_concat(name) from students group by gender having avg(age) > 30;
-- having 和 where 的区别
having 对于分组之后的数据 做进一步的筛选
where 是对于源数据的筛选操作
如果有having 就一定有group by 有group by 不一定有having
-- 分页
-- limit start, count
-- start 开始, 默认是0 可以省略, 跳过多少条数据
-- count, 获取多少条数据
select * from students limit 5;
已知用户选择的页码为n(n从1开始), 每页显示m条数据
limit (n - 1) * m, m;
第1页
select * from students limit 0,5;
第2页
select * from students limit 5,5;
第3页
select * from students limit 10,5;
-- 每页显示4个,显示第3页的信息, 按照年龄从小到大排序
select * from students limit 8,4 order by age asc;
select * from students order by age asc limit 8,4;
-- 连接查询 将两个表按照某种条件合并到一起
-- 查询学生的名字和学生对应的班级名字
-- 学生名: students;
-- 班级名: classes
select students.name,classes.name from students,classes where students.cls_id = classes.id;
select * from students,classes;
-- 触发笛卡尔积查询 效率很低
-- 内连接查询
-- 连接查询 把A表和B表按照某种条件连接起来
-- A inner join B... on A.cls_id = B.id 内连接查询
select students.name, classes.name from students inner join classes on students.cls_id = classes.id;
select s.name, c.name from students as s inner join classes as c on s.cls_id = c.id;
select * from students as s inner join classes as c on s.cls_id = c.id;
-- 按照要求显示姓名、和学生对应的班级的名字(学生所在的班级)
select s.name, c.name from students as s inner join classes as c on s.cls_id = c.id;
-- 在以上的查询中,将班级名字显示在第1列
select c.name, s.name from students as s inner join classes as c on s.cls_id = c.id;
-- 查询 学生所在的班级, 按照班级进行排序
select c.name, s.name from students as s inner join classes as c on s.cls_id = c.id order by s.name asc;
-- 外连接查询: left join + right join
-- left join 左外连接查询
-- 查询每位学生对应的班级信息
-- 左表作为主表 最左边的表作为主表 主表中的所有的数据都会显示(满足条件+不满足条件)
select * from students as s left join classes as c on s.cls_id = c.id;
-- right join 右外连接查询 使用的比较少
-- 将数据表名字互换位置,用left join完成
-- 扩充了解
-- 内连接和外连接的其他写法
-- 内连接的其他写法
select c.name, s.name from students as s join classes as c on s.cls_id = c.id;
select c.name, s.name from students as s cross join classes as c on s.cls_id = c.id;
-- 外连接的其他写法
select * from students as s left outer join classes as c on s.cls_id = c.id;
-- 子查询 select .... select
-- 查询班级中最高身高的学生信息
-- 需要在小括号之中, 优先执行
-- 是一个能够独立执行的sql语句
-- 标量子查询 子查询得到的结构 是一行一列
-- 查询班级最高身高的学生信息
select * from students where height = (select max(height) from students);
-- 查询学生的平均身高
select avg(height) from students;
-- 查询出高于平均身高的信息
select * from students where height > (select avg(height) from students);
- 列级子查询 查询的结果是一列多行
-- 查询哪些班级有学生 查找的是班级的名字
select classes.name from classes where classes.id in (select cls_id from students);
-- 通过连接查询来实现
select distinct classes.name from classes inner join students on classes.id = students.cls_id;
-- 查找哪些班级没有学生
select classes.name from classes where classes.id not in (select cls_id from students);
-- 行级子查询
-- 查找班级年龄最大,身高最高的学生 需要查找两个关键的特征同时出现在一个人身上
select * from students where (height, age) = (select max(height),max(age) from students);
-- 表级子查询 子查询语句查询出来的结果是多行多列
-- select * from students;
# 错误 select * from (select * from students); -- 需要起别名
-- 数据库备份与恢复
-- 备份 将某一台主机的数据完成一个副本的备份 mysqldump 是liunx下的指令
mysqldump -uroot -p python > ~/Desktop/python_restore.sql
-- 恢复 通过xxx.sql 文件将数据恢复到指定的数据库
1. 创建一个新的数据库
2. 执行指令恢复数据
mysql -uroot -p python_res < ~/Desktop/python_restore.sql
-- ui设计 美工
-- 数据库设计
-- 商品信息
-- 顾客信息
-- 支付信息
-- 配送信息 --> 配送员
-- 三范式
-- E-R模型
-- 类似京东商城
-- 求所有商品的平均价格,并且保留两位小数
select round(avg(price),2) from goods;
-- 查询所有价格大于平均价格的商品,并且按价格降序排序
-- 标量子查询
select * from goods where price > (select round(avg(price),2) from goods) order by price desc;
-- 查询类型cate_name为 '超级本' 的商品名称、价格
select name, price from goods where cate_name = "超极本";
select name, price from goods where cate_name = "超级本";
-- 显示商品的种类
select cate_name from goods group by cate_name;
-- 显示每种类型的商品的平均价格
select cate_name, avg(price) from goods group by cate_name;
-- 查询每种类型的商品中 最贵、最便宜、平均价、数量
select cate_name, max(price),min(price),avg(price), count(*) from goods group by cate_name;
-- 查询每种类型中最贵的商品信息
select * from goods inner join (select cate_name, max(price) as max_price,min(price),avg(price), count(*) from goods group by cate_name) as temp on goods.cate_name = temp.cate_name and goods.price = temp.max_price;
\G在SQL语句中最后面 不需要加 ; 能够让查询的结果集合中的每一列单独成行显示
-- 通过行级子查询也可以实现该需求
select * from goods where (cate_name,price) in (select cate_name, max(price) from goods group by cate_name);
-- 创建商品分类表
create table if not exists goods_cates(
id int unsigned primary key auto_increment,
name varchar(40) not null
);
-- insert into xx表 () values (), (),...; 需要手动写比较麻烦
-- insert ... select
-- 子查询语句
-- 在sql中所有的语句都称为查询语句
-- 结构化查询语言
-- 查询到的数据的字段需要和插入数据表指定的字段一一对应
insert into goods_cates (name) select cate_name from goods group by cate_name;
-- 连接更新
-- 根据goods_cates 表 更新goods 表
update goods inner join goods_cates on goods.cate_name = goods_cates.name set goods.cate_name = goods_cates.id;
-- 修改表结构
alter table goods change cate_name cate_id int unsigned not null;
-- brand_name 品牌名称
-- 创建表 并且插入数据 一步到位 create ... select
-- 如果查找的数据的字段和表中的字段名不一样 此时会在表中创建一个新的名字一样的字段,将查询到的数据一一对应的自动创建的字段上面
-- 给brand_name 起别名, 或者将goods_brands表中的name 修改为brand_name
create table if not exists goods_brands(
id int unsigned primary key auto_increment,
name varchar(40) not null
) select brand_name as name from goods group by brand_name;
--- 根据goods_brand表 来更新 goods表
update goods inner join goods_brands on goods.brand_name = goods_brands.name set goods.brand_name = goods_brands.id;
-- 修改表结构
alter table goods change brand_name brand_id int unsigned not null;
insert into goods (name,cate_id,brand_id,price)
values('惠普LaserJet Pro P1606dn 黑白激光打印机', 12, 4,'1849');
-- 连接多张表完成查询
-- 显示商品的完整信息
-- 连接多张表直接inner join 不需要and 或者 ,
select g.id,g.name,c.name,b.name,g.price from goods as g inner join goods_cates as c on g.cate_id = c.id inner join goods_brands as b on g.brand_id = b.id;
-- left join 左外连接查询
select g.id,g.name,c.name,b.name,g.price from goods as g left join goods_cates as c on g.cate_id = c.id left join goods_brands as b on g.brand_id = b.id;
select * from goods as g left join goods_cates as c on g.cate_id = c.id left join goods_brands as b on g.brand_id = b.id;
-- 外键
-- 对于已经存在的表 设置外键约束--> 更新
-- 给goods表中的 brand_id 添加外键约束, goods表已经存在, 修改约束应该是用alter sql 语句
-- references: 关联或者引用
alter table goods add foreign key(brand_id) references goods_brands(id);
alter table goods add foreign key(cate_id) references goods_cates(id);
能否在创建表的时候就设置外键约束?
-- 在添加 cate_id外键约束的时候 必须是goods_cates表已经被创建才能够添加外键约束
-- 删除外键
-- alter table goods drop foreign key 外键约束的名字;
-- 如何获取外键约束的名字
alter table goods drop foreign key goods_ibfk_1;
-- 外键名称从表的创建语句中来查看
show create table goods;
mysql学习笔记
最新推荐文章于 2024-07-25 09:12:06 发布