• <tt id="0muie"><acronym id="0muie"></acronym></tt>
  • 系統城裝機大師 - 固鎮縣祥瑞電腦科技銷售部宣傳站!

    當前位置:首頁 > 數據庫 > Mysql > 詳細頁面

    Mysql的DQL查詢操作全面分析講解

    時間:2022-12-06來源:www.djmaza-info.com作者:電腦系統城

    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]

    具體操作

    數據準備

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    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');

    簡單查詢

    1
    2
    3
    4
    5
    6
    7
    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;

    運算符

    算術運算符

    1 select name,price *1.5 newprice from data;

    條件查詢

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    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

    排序查詢

    1
    2
    3
    4
    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;

    聚合查詢

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    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值的處理

    分組查詢

    1
    2
    select sum(price) from data group by type;
    select type,count(id) from data group by type;

    條件篩選

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

    分頁查詢

    分頁顯示

    1
    2
    3
    select * from data limit 5;
    -- 從第四條開始依次向后顯示五條
    select * from data limit 3,5;

    insert into select語句

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    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;

    總結

    到此這篇關于Mysql的DQL查詢操作全面分析講解的文章就介紹到這了

    分享到:

    相關信息

    • Mysql去重的幾種方式分步講解

      我們做數據分析的時候經常會遇到去重問題,下面總結 sql 去重的幾種方式,后續如果還有再補充,大數據分析層面包括 hive、clickhouse 也可參考。...

      2022-12-06

    • 安裝MYSQL端口被占用

      很多使用電腦的小伙伴安裝MYSQL端口被占用的情況,這個時候我們該如何解決呢?今天小編帶來了詳細的解決方法,主要在命令窗口里進行設置就可以了,具體的教程一起來看看吧。...

      2022-10-24

    系統教程欄目

    欄目熱門教程

    人氣教程排行

    站長推薦

    熱門系統下載

    人人透人人躁人人躁看_人人网在线观看国产_人人做人人爱超碰首页 <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <文本链> <文本链> <文本链> <文本链> <文本链> <文本链>