MYSQL基础 DQL语言-增删改查、排序、分页查询、聚合函数、分组查询、sql的执行顺序

1.DQL语言

针对的表中内容的操作。增加insert 删除 delete 修改 update 查询 select

1.1 添加数据

语法:1. 语法: insert into  表名(列名,列名,....列名) values(值,值.....值);
   注意: 列个数必须和值个数匹配。 符合约束。

例:

-- 添加数据employee
insert into employee(id,name,age,salary,entrydate,sex);--只运行本行时,分号可以不加,加分号是为了多条数据一起运行时不报错
values(1,'小明',16,55555,'2020-10-11','男');

insert into employee(name,age,salary,entrydate) 
values('小亮',19,66666,'2019-11-11');

-- 批量添加
insert into employee(name,age,salary,entrydate,sex)
values('小洪',18,77777,'2018-11-12','男'),
('小齐',18,67777,'2019-11-12','男'),
('小张',28,87777,'2017-11-12','男'),
('小李',26,97777,'2018-11-12','男');

1.2 删除记录

语法: delete from 表名 where 条件;
  意思: 从指定表中删除满足条件的数据。

-- ----删除id号为3员工-----employee->员工表
delete from employee where id=3;
-- 删除如果不使用条件--删除所有记录 
delete from employee;  

1.3 修改

语法: update 表名 set 列名=值,列名=值... where 条件;

-- 修改一列,修改employee表中id=10的那一行中的age的值,age=19
update employee set age=19 where id=10;
-- 修改多列
update employee set name='郭红霞',sex='女' where id=9;
-- 全部记录都修改,没有加条件,表中所有的name,sex都会改变为‘小明’、‘男’
update employee set name='小明',sex='男'

1.4 查询

1. 基本查询

语法: select 列名,列名,....列名 from 表名;

-- 查询指定列的值,查询employee表中的name、salary两列
select name,salary from employee;

-- 查询所有列: *表示通配符 不建议大家使用*;影响性能(减慢查询速度)。建议把所有的列展示
select * from employee; -- *底层转换为所有的列。 
select id,name,age,salary,entrydate,sex from employee; 

-- 查询列并另起名字,as:name=xm,查询结果会显示xm、age两列
select name as xm,age from employee; 
-- 另起名时可以省略as
select name xm,age nl from employee;

-- 查询时可以使用运算符。后期某些字段如果可以通过其他字段计算出来,设计表时无需增加该字段。
select name,salary+salary*0.1 as xinzi from employee;

-- 订单表: id,num,price,total.  order它是一个关键字 我们可以使用``,即`order`,这样就不会报错
create table `order`(
   id int primary key auto_increment,
	 num int,
	 price decimal(9,2)
);
--给order表中的num、price添加三个数据(2,188),(3,99),(2,199)

insert into `order`(num,price) values(2,188),(3,99),(2,199);
--查询order中的所有数据
select * from `order`
--查询order中的id,num,price,num*price as total
select id,num,price,num*price as total from `order`



-- 查询时去除重复的字段值;查询性别的类型,关键字:distinct
select  distinct sex from employee;

2. 条件查询

1. 比较: >,>=,<,<=,!=,=
-- 查询年龄大于20的学生
select * from student where age>20;
-- 查询年龄不等于25
select * from student where age!=25;


2. 多条件 and  or
-- 查询家庭地址为北京且年龄小于19
select * from student where address='北京' and age<19;
-- 查询编号为3 编号为6 和编号为9
select * from student where id=3 or id=6 or id=9

3. 范围条件:
-- 查询年龄在20~30之间的学生。
select * from student where age>=20 and age<=30;
-- 可以between and 
select * from student where age between 20 and 30;

-- 查询编号为3 编号为6 和编号为9
select * from student where id=3 or id=6 or id=9
-- 上面可以in
select * from student where id in(3,6,9); 

4: 模糊查询。like
-- 查询姓张. 如果模糊查询时只使用了like 而没有使用通配符 就和普通查询没有区别。
-- 通配符: %:表示统配n的字符   _:表示统配一个字符
--表示:张XXX(姓张就行)
select * from student where name like '张%'
-- 查询名字中含有三
select * from student where name like '%三%'
-- 查询姓张且只有两个
select * from student where name like '张__'

3. 非空

-- 出现内容为null
select * from student where address  is  null;

--非空

select * from student where address  is not null;

1.5 排序

语法:order by 排序列名,,,,[desc降序]

-- 查询学生信息并且按照年龄排序--升序,不加关键字,默认就是升序
select * from student order by age;
-- 查询学生信息并且按照年龄排序--降序
select * from student order by age desc;

-- 如果第一个列值相同,再按照第二列排序...
select * from student order by age desc,name desc;

1.6 分页查询

格式:-- 分页的规律: pageSize:每页条数 current:当前的页码
-- select * from 表名 limit (current-1)*pageSize,pageSize;--参考上网时,网页中下面的分页

 

--  0:表示起始记录  5:每页展示的条数,查询输出:从第一条开始到第五条
select * from student limit 0,5;
-- 查询第二页 并且每页显示5条
select * from student limit 5,5;
-- 查询第三页 并且每页显示5条
select * from student limit 10,5;
-- 查询第四页 并且每页显示5条
select * from student limit 15,5;

1.7 聚合函数

SQL结构化查询语言,它既然是编程语言,那么它也有函数。自带函数(程序自带)和自定义函数(程序员)
聚合函数就属于内置的函数。5个.
max(列):最大数
min(列):最小数
sum(列):求和
avg(列):平均值
count(列):计数

-- 获取最大年龄值,最小年龄值,年龄和,平均年龄
select max(age),min(age),sum(age),avg(age) from student;
-- 求学生的个数
select count(id) from student;

1.8 分组查询

在sql中有个 group by  语句  将某一列相同数据 视为一组 然后进行查询
与聚合函数连用

-- 各个地方的最大年龄,address='',注意不是null,是空格
select address,min(age) from student where address!='' group by address;

-- 分组条件 having 
-- 求各个区域最小年龄大于30的
select address,min(age) from student where address!=''  group by address having min(age)>30;

sql的执行顺序

sql语法:
   select distinct *  from 表名 
       where 条件
       group by 分组
       having 分组条件
       order by 排序
       limit 分页。
 
上面为他得语法结构。顺序不能乱。 

mysql执行引擎:执行得顺序。
1、from子句识别查询表的数据;

2、where子句基于指定的条件对记录进行筛选;

3、group by 子句将数据划分成多个组别,如按性别男、女分组;

4、有聚合函数时,要使用聚集函数进行数据计算;

5、Having子句筛选满足第二条件的数据;

6、执行select语句进行字段筛选

7、筛选重复数据;

8、对数据进行排序;

9、执行limit进行结果限定

-- 验证了where的执行在select前
select name as xm from student where xm='张三'     --报错,此时name as xm还没有执行
-- 验证了where的执行在聚合前
select * from student where age=max(age)   --调用where时,age=max(age)还没有运行
-- 聚合函数的执行在having前 --正常运行
select address,max(age) from student group by address
having max(age)>30;

-- 验证select在order by之前执行
select name as xm from student order by xm;

小测试:

--创建product表
CREATE TABLE product(
	pid INT PRIMARY KEY,-- 主键ID
	pname VARCHAR(20),-- 商品名称
	price DOUBLE,-- 商品价格
	category_name VARCHAR(32)-- 商品分类名称
);
//赋值
INSERT INTO product(pid,pname,price,category_name) VALUES(1,'联想电脑',5000,'电脑办公');
INSERT INTO product(pid,pname,price,category_name) VALUES(2,'海尔电脑',3000,'电脑办公');
INSERT INTO product(pid,pname,price,category_name) VALUES(3,'雷神电脑',5000,'电脑办公');

INSERT INTO product(pid,pname,price,category_name) VALUES(4,'JACK JONES',800,'服装');
INSERT INTO product(pid,pname,price,category_name) VALUES(5,'真维斯',200,'服装');
INSERT INTO product(pid,pname,price,category_name) VALUES(6,'花花公子',440,'服装');
INSERT INTO product(pid,pname,price,category_name) VALUES(7,'劲霸',2000,'服装');

INSERT INTO product(pid,pname,price,category_name) VALUES(8,'香奈儿',800,'女士用品');
INSERT INTO product(pid,pname,price,category_name) VALUES(9,'相宜本草',200,'女士用品');
INSERT INTO product(pid,pname,price,category_name) VALUES(10,'面霸',5,'女士用品');

INSERT INTO product(pid,pname,price,category_name) VALUES(11,'雪碧',56,'饮料饮品');
INSERT INTO product(pid,pname,price,category_name) VALUES(12,'香飘飘奶茶',1,'饮料饮品');

INSERT INTO product(pid,pname,price,category_name) VALUES(13,'iPhone9',8000,NULL);


-- 查询product表中所有记录

select pid,pname,price,category_name from product;

-- 查询product表中pid和pname字段

select pid,pname from product;

-- 查询结果是表达式(运算查询):将所有商品的价格+10元进行显示.

select price+10 from product 

-- 查询商品名称为“花花公子”的商品所有信息:

select pid,pname,price,category_name from product where pname='花花公子';

-- 查询价格为800商品

select pid,pname,price,category_name 
from product 
where price in (800);

-- 查询价格不是800的所有商品

select pid,pname,price,category_name 
from product 
where price !=800;

-- 查询商品价格大于60元的所有商品信息

select pid,pname,price,category_name
from product
where price>60;

-- 查询商品价格在200到1000之间所有商品

select pid,pname,price,category_name
from product
where price between 200 and 1000;

-- 查询商品价格是200或800或者2000的所有商品

select pid,pname,price,category_name
from product
where price=200 
or price=800 
or price=2000;

-- 查询含有'霸'字的所有商品

select pid,pname,price,category_name
from product
where pname like '%霸%';

-- 查询以'香'开头的所有商品

select pid,pname,price,category_name
from product
WHERE pname like '香%';

-- 查询第二个字为'想'的所有商品

select pid,pname,price,category_name
from product
where pname like '_想%';

-- 商品没有分类的商品

select pid,pname,price,category_name
from product 
where category_name is null;

-- 查询有分类的商品

select pid,pname,price,category_name 
from product
where category_name is not null;

-- 1.使用价格排序(降序)

select pid,pname,category_name,price
from product
order by price ;

-- 2.在价格排序(降序)的基础上若价格相同,相同价格的数据以pid降序排序

select pid,pname,category_name,price
from product
order by price desc,pid desc;

-- 求出商品的最高价格值,最低价格值,平均价格,以及所有商品的价格和

select max(price),min(price),avg(price),sum(price)
from product;

-- 求出每类商品的最高价格,最低价格。

select category_name,max(price),min(price)
from product
group by category_name;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值