MySQL 彙總函式、運算子操作、約束

來源:互聯網
上載者:User

標籤:數值   mos   查詢   運算   str   ike   def   family   完整性   

4、彙總函式

1、分類
  avg(欄位名) : 求該欄位平均值
  sum(欄位名) : 求和
  max(欄位名) : 最大值
  min(欄位名) : 最小值
  count(欄位名) : 統計該欄位記錄的個數
2、樣本
  1、攻擊力最強值是多少
    select max(gongji) from MOSHOU.sanguo;
  2、統計id 、name 兩個欄位分別有幾條記錄
    select count(id),count(name) from sanguo;
    ## 空值 NULL 不會被統計,""會被統計

  3、計算蜀國英雄的總攻擊力
    select sum(gongji) from MOSHOU.sanguo
    where country="蜀國";
  4、統計蜀國英雄中攻擊值大於200的英雄的數量
    select count(*) from MOSHOU.sanguo
    where gongji>200 and country="蜀國";

 

4、運算子操作
  1、數值比較/字元比較
    1、數值比較 := != > >= < <=
    2、字元比較 := !=
    3、練習
      1、尋找攻擊力高於150的英雄的名字和攻擊值
        select name,gongji from sanguo where gongji>150;
      2、將趙雲的攻擊力設定為360,防禦力設定為68
        update sanguo set gongji=360,fangyu=68
        where name="趙雲";

5、查詢表記錄時做數學運算
  1、運算子
    + - * / % 
  2、樣本
    1、查詢時所有英雄攻擊力翻倍
      select id,name,gongji*2 as gj from sanguo;
2、邏輯比較
    1、and (兩個或多個條件同時成立)
    2、or (任意一個條件成立即可)
    3、練習
      1、找出攻擊值高於200的蜀國英雄的名字、攻擊力
        select name as n,gongji as g from sanguo
        where gongji>200 and country="蜀國";
      2、將吳國英雄中攻擊值為110的英雄的攻擊值改為100,防禦力改為60
        update sanguo set gongji=100,fangyu=60
        where country="吳國" and gongji=110;
      3、尋找蜀國和魏國的英雄資訊
        select * from sanguo 
        where country="蜀國" or country="魏國";
3、範圍內比較
1、between 值1 and 值2
    2、where 欄位名 in(值1,值2,...)
    3、where 欄位名 not in(值1,值2,...)
    4、練習
1、尋找攻擊值100-200的蜀國英雄資訊
        select * from sanguo
        where gongji between 100 and 200 and
        country="蜀國";
      2、找到蜀國和吳國以外的國家的女英雄資訊
        select * from sanguo
        where country not in("蜀國","吳國") 
        and sex="女";
      3、找到id為1、3或5的蜀國英雄 和 貂蟬的資訊
        select * from sanguo
        where 
        (id in(1,3,5) and country="蜀國") or name="貂蟬";
      4、匹配空、非空
        1、空 :where name is null
        2、非空:where name is not null
        3、樣本
          1、姓名為NULL值的蜀國女英雄資訊
            select * from sanguo
            where
            name is null and country="蜀國" and sex="女";
          2、姓名為 "" 的英雄資訊
            select * from sanguo where name="";
        4、注意
          1、NULL :空值,只能用 is 或者 is not 去匹配
          2、"" :Null 字元串,用 = 或者 != 去匹配
        5、模糊比較
          1、where 欄位名 like 運算式
          2、運算式 
            1、_ : 匹配單個字元
            2、% : 匹配0到多個字元
          3、樣本
          select name from sanguo where name like "_%_";
          select name from sanguo where name like "%";
          ## NULL不會被統計,只能用is、is not去匹配
          select name from sanguo where name like "___";
          select name from sanguo where name like "趙%";

2、約束
  1、作用 :保證資料的完整性、一致性、有效性
  2、約束分類
    1、預設約束(default)
      1、插入記錄,不給該欄位賦值,則使用預設值
    2、非空約束(not NULL)
      1、不允許該欄位的值有NULL記錄
      sex enum("M","F","S") not null defalut "S"

MySQL 彙總函式、運算子操作、約束

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.