Mysql 練習 總結

來源:互聯網
上載者:User

標籤:串連查詢   lap   cte   回顧   display   整數   open   個學生   函數   

一. 總結

首先回顧: 串連查詢(幾張表連在一起  組成一個大表 在進行查詢 ---易忘) 子查詢   聯集查詢    詳見  基礎篇

建表順序:首先  建主表  再建從表   填寫內容也一樣  先填寫主表 在填寫 從表

 

 

小括弧:

1. 小括弧 可以表示   獨立運算的部分  

2.帶小括弧的是方法  ()  比如  count();  where  尋找指定行: 

 

#---查詢95033班和95031班全體學生的記錄。select * from  Student ,Course, Score where Student.Sno=Score.Sno and Course.Cno=Score.Cno and ( class=‘95033‘or class=‘95031‘)

關鍵詞 :  

 逗號   很重要;   所有語句  幾乎都可以嵌套使用

 select.. from   ; where   ; group by    having  ;   order by 

distanct    ; or    ;in  ;   and   ;  not 

 

1.not  :  not in (看前面 mysqle 基礎)  和 not like

 #- 查詢Student表中不姓“王”的同學記錄。 select * from Student where Sname not like‘王%‘ 

2. group by  和  where  , count(*)連用的時候 注意  [帶小括弧的表示方法}

首先定義:聚集合函式:一種函數,它對一組行中的某個列執行計算,並返回單個值。1.where 和 having簡單的講,where的條件是欄位;而having 的條件可以是欄位,也可以是聚集合函式;重要的是,where是篩選來源資料,having多與group by 一起使用,並且條件常常是聚集合函式;當有group by 時,having在group  by 條件的後面,而where 在group by的前面。聚集合函式:sum,count,avg ...等等;2.count和sumcount 是‘累計’;  sum是‘累加’;還是上面的表a_info中;查詢每個年級中分數大於60的有多少人以及他們的平均分,總分是多少:    3.sql語句的執行順序:(1)from    選取資料來源;(2)where  篩選資料來源;(3)  group  by 將篩選的資料來源分組;(4)使用聚集合函式計算;(5)having 篩選分組的資料;(6)計算運算式;(7)order by 排序;
group where count(*)同時使用講解
#----查詢至少有2名男生的班號。select class from Student  where Ssex=‘男‘ group by class having count(*)>=2

3.distanct: 使用方法 見  基礎部分  不做贅述

4.擷取時間 年月日 詳見 下一篇  部落格   以下簡略 

擷取的 為整數 可以進行大小比較:

擷取年  year(時間所在列名)----列名不加引號

擷取 目前時間  date(now())

擷取 天        date(時間所在的列)

#----查詢Student表中每個學生的姓名和年齡。select Sname,year(now())-year(Sbrithday ) from Student#---查詢Student表中最大和最小的Sbirthday日期值。select Sname, max(date(sbrithday)) from Studentunionselect Sname, min(date(sbrithday)) from Student#---以班號和年齡從大到小的順序查詢Student表中的全部記錄。select * from Student order by class,year(Sbrithday); 

 

    

 

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.