• 一篇文章带你学完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

    具体操作

    数据准备

    1. create database if not exists test;
    2. use test;
    3. create table if not exists data(
    4. id tinyint primary key auto_increment,
    5. price double NOT null,
    6. name varchar(20) not null,
    7. type varchar(20) not null)
    8. ;
    9. insert into data values
    10. (null,900,'洗衣机','b'),
    11. (null,1900,'冰箱','b'),
    12. (null,2900,'空调','b'),
    13. (null,3900,'电视','b'),
    14. (null,150,'衣服','c'),
    15. (null,180,'裤子','c'),
    16. (null,200,'鞋子','c'),
    17. (null,188,'洗面奶','a'),
    18. (null,188,'洗发水','a'),
    19. (null,199,'洗衣液','a'),
    20. (null,88,'沐浴露','a'),
    21. (null,5,'泡面','d'),
    22. (null,15,'饼干','d'),
    23. (null,30,'咖啡','d');

    76b742d8c78f491db784cff0d70b1a65.png

     

    简单查询

    626d2226b2044168a6825a6d53b292e6.png

    1. select * from data;
    2. select name,price from data;
    3. select * from data as d;
    4. select * from data d;
    5. select d.name,d.price from data d;
    6. select distinct price from data;
    7. select name,price +100 newprice from data;

    运算符

    8e2fb6fc2d124869b42d601169e8dc0f.png

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

    条件查询

    1. select * from data where name='洗衣机';
    2. select * from data where !(price>100);
    3. select * from data where price between 200 and 1000;
    4. select * from data where price in(188,900);
    5. -- 等于下面两句
    6. select * from data where price = 188 or price =900;
    7. select * from data where price = 188 || price =900;
    8. select * from data where name like '%衣%';
    9. select * from data where name like '衣%';
    10. select * from data where name like '_衣%';
    11. select * from data where id is null;

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

    排序查询

    b1dbe9926851494abb4f6ab9e2f6c5a7.png

    1. select * from data order by price;
    2. select * from data order by price desc;
    3. select distinct price from data order by price desc;
    4. select * from data order by price,id;

    聚合查询

    ec060b8c4311432d9ff89d6cc2f513b7.png

    1. select count(*) from data;
    2. -- 不全为空的行数
    3. select count(id) from data;
    4. -- 通过主键值查询行数
    5. select count(*) from data where price<200;
    6. select sum(price) from data where type='A';
    7. select max(id) from data;
    8. select min(price) from data;
    9. select max(price) max_price,min(price) min_price from data;
    10. select avg(price) from data where type='c';

    null值的处理

    9e99afd9d1e74450b640df7623041ece.png

    分组查询

    bd395123e66c4eb895f89726fabcbecf.png

    910e0dbd165d4701a23fbbd14a785581.png

    1. select sum(price) from data group by type;
    2. 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

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

    insert into select语句 

    51fcc9371a284c90896d4a43cfe4385e.png

    1. create table data2(
    2. name varchar(10),
    3. price double);
    4. insert into data2 select name,price from data;
    5. select * from data2;
    6. create table data3(
    7. type varchar(10),
    8. num int
    9. );
    10. insert into data3 select type,count(*) from data group by type order by count(*);
    11. select * from data3;

    总结

    7debda33d8d646ba949030c9435d8841.png

     

  • 相关阅读:
    时间序列聚类的直观方法
    日志审计设计-结合spring-aop实现
    算法-有效括号
    sed应用
    【学习笔记】:Maven初级
    C#实现FFT(递归法)
    MySQL之优化SELECT语句
    FIddler抓手机的通讯包的设置记录
    【AcWing】827. 双链表
    var、let和const
  • 原文地址:https://blog.csdn.net/qq_63701832/article/details/127933134