一篇文章带你学完mysql的DQL查询操作

目录

DQL简介

具体操作

数据准备

简单查询

运算符

条件查询

排序查询

聚合查询

null值的处理

分组查询

分页查询

insert into select语句 

总结


 

DQL简介

概念:DQL(data query language)数据查询语言 select操作

排序规则:

 - select 表达式1|字段,.... - from 表名 where 条件 - group by 列名 - having 条件 - order by 列名 asc|desc - limit 位置,数量

语法结构:

    SELECT [ALL | DISTINCT] ALL表示查询出所有的内容 DISTINCT 去重
            {* | 表名.* | 表名.字段名[ AS 别名][,...]} 指定查询出的字段的
        FROM
            表名[AS 别名][,表1... AS 别名]
        [INNER | [LEFT | RIGHT] [OUTER] JOIN 另一张表名 [AS 别名] ON 关联条件]
        [WHERE 条件]
        [GROUP BY 分组字段[,...]] 
        [HAVING 给分组后的数据进行条件筛选]
        [ORDER BY 排序字段[,...]]
        [LIMIT [startIndex,]pageSize]

e98e693d53c14af99017f921782bf54e.png

具体操作

数据准备

create database if not exists test;
use test;
create table if not exists data(
id tinyint primary key auto_increment,
price double NOT null,
name varchar(20) not null,
type varchar(20) not null)
;
insert into data values
(null,900,'洗衣机','b'),
(null,1900,'冰箱','b'),
(null,2900,'空调','b'),
(null,3900,'电视','b'),
(null,150,'衣服','c'),
(null,180,'裤子','c'),
(null,200,'鞋子','c'),
(null,188,'洗面奶','a'),
(null,188,'洗发水','a'),
(null,199,'洗衣液','a'),
(null,88,'沐浴露','a'),
(null,5,'泡面','d'),
(null,15,'饼干','d'),
(null,30,'咖啡','d');

76b742d8c78f491db784cff0d70b1a65.png

 

简单查询

626d2226b2044168a6825a6d53b292e6.png

select * from data;

select name,price from data;

select * from data as d;
select * from data d;
select d.name,d.price from data d;
select  distinct price from data;

select name,price +100 newprice from data;

运算符

8e2fb6fc2d124869b42d601169e8dc0f.png

  • 算术运算符
select name,price *1.5 newprice from data;

条件查询

select * from data where name='洗衣机';
select * from data where  !(price>100);
select * from data where price between 200 and 1000;
select * from data where price in(188,900);
-- 等于下面两句
select * from data where price = 188 or price =900;
select * from data where price = 188 || price =900;
select * from data where name like '%衣%';
select * from data where name like '衣%';
select * from data where name like '_衣%';
select * from data where id is null;

注释:当有NULL作为比较大小的对象时,最大值和最小值均为null

排序查询

b1dbe9926851494abb4f6ab9e2f6c5a7.png

select * from data order by price;
select * from data order by price desc;
select distinct price from data order by price desc;
select * from data order by price,id;

聚合查询

ec060b8c4311432d9ff89d6cc2f513b7.png

select count(*) from data;
-- 不全为空的行数

select count(id) from data;
-- 通过主键值查询行数

select count(*) from data where price<200;
select sum(price) from data where type='A';
select max(id) from data;
select min(price) from data;
select max(price) max_price,min(price) min_price from data;
select avg(price) from data where type='c';

null值的处理

9e99afd9d1e74450b640df7623041ece.png

分组查询

bd395123e66c4eb895f89726fabcbecf.png

910e0dbd165d4701a23fbbd14a785581.png

select sum(price) from data group by type;
select type,count(id) from data group by type;
  • 条件筛选

540b6cddb18f468f9ae2fa333b103092.png

select type,count(id) count from data group by type having count=4 order by type;

分页查询

3049cee964864b23ba68c24220c9eae5.png

  •  分页显示

d1d6d634d2b64b908a2c399131dcf107.png

select * from data limit 5;
-- 从第四条开始依次向后显示五条
select * from data limit 3,5;

insert into select语句 

51fcc9371a284c90896d4a43cfe4385e.png

create table data2(
name varchar(10),
price double);
insert into data2 select name,price from data;
select * from data2;

create table data3(
type varchar(10),
num int
);
insert into data3 select type,count(*) from data group by type order by count(*);
select * from data3;

总结

7debda33d8d646ba949030c9435d8841.png

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

coleak

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值