小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

人工智能(mysql)—— mysql完整的sql查詢

 老張的菜地 2019-08-16

一、SQL查詢

        SQL查詢的執(zhí)行順序,先對表中現(xiàn)有的字段進(jìn)行篩選(1、where),再進(jìn)行聚合分組(2、group),然后根據(jù)查詢條件進(jìn)行查找(3、select),緊接著對篩選的結(jié)果在再篩選(4、having),接下來根據(jù)條件進(jìn)行排序(5、order),最后對記錄的顯示進(jìn)行限制(6、limit)。
            3、select ...聚合函數(shù) from 表名
            1、where ...
            2、group by ...
            4、having ...
            5、order by ...
            6、limit ...;

    1、order by

            給查詢的結(jié)果進(jìn)行排序
            語法:order by 字段名 排序方式
            排序方式
                    a、升序 :ASC(默認(rèn))
                    b、降序 :DESC

  1. 1、將蜀國的英雄按照攻擊值從高到低排序
  2. select * from sanguo
  3. where country='蜀國'
  4. order by gongji DESC;
  5. 2、將魏蜀兩國的男英雄中名字為三個字符的英雄按防御值升序排列
  6. select * from sanguo
  7. where country in('蜀國','魏國') and sex='男' and name like '___'
  8. order by fangyu ASC;

    2、limit(永遠(yuǎn)放在SQL語句的最后)

            限制顯示查詢記錄的個數(shù)

        a、用法

                1)limit n  -->顯示n條記錄
                2)limit m,n  
                        m :表示從 m+1 條記錄開始顯示
                        n :表示顯示 n 條
                        limit 2,4 :顯示第3、4、5、6四條記錄
                        limit 0,2 :顯示第1、2兩條記錄

        b、示例
  1. 1、在蜀國英雄中查找攻擊值前三名且名字不為NULL的英雄姓名、攻擊值和國家
  2. select name,gongji,country from sanguo
  3. where country='蜀國' and name is not NULL
  4. order by gongji DESC
  5. limit 3;
  6. 2、在蜀國英雄中,查找防御值倒數(shù)第2名至倒數(shù)第4名的英雄信息
  7. select * from sanguo
  8. where country='蜀國' order by fangyu limit 1,3;
        c、分頁查詢

                每頁顯示5(n)條記錄,顯示第4(m)頁
                        第1頁:limit 0,5   ## 1 2 3 4 5
                        第2頁:limit 5,5   ## 6 7 8 9 10          ## (2-1)*5
                        第3頁:limit 10,5 ## 11 12 13 14 15  ##(3-1)*5
                        第4頁:limit 15,5 ## 16 17 18 19 20  ##(4-1)*5
            
                分頁公式:limit (m-1)*n,n  m:第幾頁 n:每頁顯示記錄條數(shù)

    3、聚合函數(shù)

                avg(字段名) : 平均值
                max(字段名) : 最大值
                min(字段名) : 最小值
                sum(字段名) : 求和
                count(字段名) : 統(tǒng)計(jì)該字段記錄的個數(shù)

  1. 1、攻擊力最強(qiáng)值
  2. select max(gongji) as best from sanguo;
  3. 2、統(tǒng)計(jì)一下表中id、name字段分別有多少條記錄
  4. select count(id),count(name) from sanguo;
  5. ## 空值NULL不會被統(tǒng)計(jì)
  6. select count(*) from sanguo;
  7. 3、統(tǒng)計(jì)蜀國英雄中攻擊值大于200的英雄的數(shù)量
  8. select count(*) from sanguo where country='蜀國' and gongji > 200;

    4、group by(先分組-再聚合-去重)

            給查詢的結(jié)果進(jìn)行分組

        a、示例

                    計(jì)算所有國家的平均攻擊力,顯示國家名和平均攻擊力

                select country,avg(gongji) from sanguo group by country; 
                        先分組  -  再聚合  -   去重

                        蜀國    
                        蜀國         400       蜀國
                        蜀國
                        魏國         300       魏國
                        魏國
                        吳國         200       吳國

  1. 查找所有國家中,英雄數(shù)量最多的前2名,顯示國家名稱和英雄數(shù)量
  2. select country,count(*) as number from sanguo
  3. group by country
  4. order by number DESC limit 2;
            b、注意

                    1)group by之后的字段名必須要為select之后的字段名
                    2)如果select后的字段名和group by之后的字段不一致,則必須對該字段進(jìn)行聚合處理(聚合函數(shù))

    5、having

                對查詢結(jié)果進(jìn)一步篩選

  1. 找出平均攻擊力大于105的國家的前2名,顯示國家名稱和平均攻擊力
  2. select country,avg(gongji) from sanguo
  3. group by country
  4. having avg(gongji)>105
  5. order by avg(gongji) DESC limit 2;

                注意
                       1)having語句通常與group by語句聯(lián)合使用,用來過濾由group by語句返回的記錄集
                       2)having語句的存在彌補(bǔ)了where條件子句不能與聚合函數(shù)聯(lián)合使用的不足,where操作的是表中實(shí)際存在的字段,having操作的是聚合函數(shù)生成的顯示列

    6、distinct

                不顯示字段的重復(fù)值

  1. 1、sanguo表中有哪些國家
  2. select distinct country from sanguo;
  3. 2、計(jì)算魏國一共有多少個英雄
  4. select count(distinct name) from sanguo
  5. where country='魏國';

                注意
                        1)distinct處理的是distinct和from之間的所有字段,所有字段的值必須完全相同才可以去重
                        2)distinct不能對任何字段做聚合處理

    7、查詢表記錄時的數(shù)學(xué)運(yùn)算

        a、運(yùn)算符

                +   -   *    /    %

        b、示例
  1. 查詢時顯示所有英雄的攻擊力 * 10
  2. select name,gongji*10,country from sanguo;

二、約束

    1、作用

            為了保證數(shù)據(jù)的完整性、一致性、有效性,可以限制無效的數(shù)據(jù)插入到數(shù)據(jù)表中

    2、約束分類

        a、默認(rèn)約束(default)
                在插入記錄時,如果不給該字段賦值,則使用默認(rèn)值
                格式:字段名 數(shù)據(jù)類型 default 默認(rèn)值,
  1. create  table  tt(sex  enum('男','女','保密') default  '保密');
  2. desc   tt ;
        b、非空約束
                不允許該字段的值有空值NULL記錄
                格式:字段名 數(shù)據(jù)類型 not null,
  1. create table xx(id int not null,name varchar(20) not null);
  2. desc xx;

三、嵌套查詢(子查詢)

        把內(nèi)層的查詢結(jié)果作為外層的查詢條件

        格式:select ... from 表名 where 字段名 運(yùn)算符(select ....);


  1. 1、把攻擊值小于 平均攻擊值 的名字和攻擊值顯示出來(分兩步)
  2. select name,gongji from MOSHOU.sanguo
  3. where gongji < (select avg(gongji) from MOSHOU.sanguo);
  4. 2、找出每個國家攻擊力最高的英雄名字和攻擊值
  5. select name,gongji from sanguo
  6. where (country,gongji) in (select country,max(gongji) from sanguo group by country);

四、附錄1:數(shù)據(jù)庫數(shù)據(jù)創(chuàng)建

為了方便,建立直接復(fù)制、黏貼以下代碼,快速創(chuàng)建數(shù)據(jù)庫和表。

  1. create database MOSHOU;
  2. use MOSHOU;
  3. create table hero(
  4. id int,
  5. name char(15),
  6. sex enum('男','女'),
  7. country char(10)
  8. )default charset=utf8;
  9. insert into hero values
  10. (1,'曹操','男','魏國'),
  11. (2,'小喬','女','吳國'),
  12. (3,'諸葛亮','男','蜀國'),
  13. (4,'貂蟬','女','東漢'),
  14. (5,'趙子龍','男','蜀國'),
  15. (6,'魏延','男','蜀國');
  16. use MOSHOU;
  17. create table sanguo(
  18. id int,
  19. name char(20),
  20. gongji int,
  21. fangyu tinyint unsigned,
  22. sex enum('男','女'),
  23. country varchar(20)
  24. )default charset=utf8;
  25. insert into sanguo values
  26. (1,'諸葛亮',120,20,'男','蜀國'),
  27. (2,'司馬懿',119,25,'男','魏國'),
  28. (3,'關(guān)6羽',188,60,'男','蜀國'),
  29. (4,'趙云666',200,66,'男','魏國'),
  30. (5,'8孫權(quán)',110,20,'男','吳國'),
  31. (6,'貂蟬',666,10,'女','魏國'),
  32. (7,null,1000,99,'男','蜀國'),
  33. (8,'',1005,88,'女','蜀國');

五、附錄2

        人工智能(mysql)—— 目錄匯總

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多