MySQL入門(二)

  • 數(shù)據(jù)準(zhǔn)備
number name age class grade
201804001 劉一 16 19 二年級
201804002 陳二 19 18 一年級
201804003 張三 20 19 二年級
201804004 李四 17 19 一年級
201804005 王五 18 19 三年級
201804006 趙六 24 18 二年級
201804007 孫七 22 19 三年級
201804008 周八 21 19 二年級
201804009 吳九 25 18 一年級
201804010 鄭十 23 19 一年級
201804011 小周周 20 18 二年級
201804012 周周周 21 19 三年級

篩選條件

  • 比較運算符
    等于:=
    大于等于:>=
    小魚等于:<=
    大于:>
    小于:<
    不等于:!=或<>
    IS NULL
    IS NOT NULL
  • 邏輯運算符
    與:and
    或:or
    非:not
    查找16到20歲的學(xué)生select columns from table_name where age>=16 and age<=20;
  • 其他操作
    排序(order by)SELECT columns FROM tb_name ORDER BY columns desc;正序:asc(默認(rèn))倒序:desc
    限制個數(shù)(limit)SELECT columns FROM tb_name LIMIT start, count ;LIMIT count;
    去重(distinct)SELECT DISTINCT columns FROM tb_name;
    模糊查詢(like'%')select columns from table_name where name like '%周_';注:%表示任意多個字符;_表示任意一個字符
  • 范圍查詢
    連續(xù)范圍: BETWEEN a AND b
    select columns from table_name where age BETWEEN 16 and 20;
    間隔返回: IN
    select columns from table_name where column in(X,X,X);

聚合與分組

  • 常用聚合函數(shù)
    統(tǒng)計個數(shù):COUNT(column)select count(name) from student;
    求和:SUM(column)
    最大值:MAX(column)
    平均值:AVG(column)
    最小值:MIN(column)
    列出字段全部值:GROUP_CONCAT(column)select group_concat(age) from student;
  • 分組查詢(group by)
    Select 字段 from 表 group by 字段;select class from student group by class;
    Select 字段,count(*) from 表 group by 字段;select class,count(*) from student group by class;
    在分組的情況下,只能夠出現(xiàn)分組字段和聚合字段,其他的字段沒有意義,會報錯!
  • 聚合篩選(having)
    select class from student group by class,age having age>=18;
    加having條件表達(dá)式,可以對輸出的結(jié)果做出限制,having有分組的作用
    假如說一個查詢語句中同時包含了別名(as),聚合函數(shù), where, having
    select * from (select class,count(*) from student where age >=20 group by class,age having age>=21) as b;
    統(tǒng)計年齡大于等于20歲的不同班級的學(xué)生的數(shù)量,對于統(tǒng)計的結(jié)果再統(tǒng)計年齡大于等于21的學(xué)生,把年齡相同的學(xué)生歸類到一起,最后把結(jié)果命名為表b展示出來
    那么他們的執(zhí)行順序是
    先是執(zhí)行:where
    然后執(zhí)行:聚合函數(shù)和別名
    最后執(zhí)行:having

子查詢(了解)

將一個查詢的結(jié)果留下來用于下一次查詢 ( select 中嵌套 select )


鏈接查詢(了解)

  • 內(nèi)連接(inner join)
    • 無條件內(nèi)連接:
      無條件內(nèi)連接,又名交叉連接/笛卡爾連接
      第一張表種的每一項會和另一張表的每一項依次組合
      Mysql> select * from student [inner] join scoren
    • 有條件內(nèi)連接:
      在無條件內(nèi)鏈接的基礎(chǔ)上,加上一個on子句
      當(dāng)連接的時候,篩選出那些有實際意義的記錄來進行組合
      Mysql> select * from student inner join scoren
      -> on dept_id = id;
  • 外連接({left | right} join)
    • 左外連接: (以左表為基準(zhǔn))
      兩張表做連接的時候,在連接條件不匹配的時候
      留下左表中的數(shù)據(jù),而右表中的數(shù)據(jù)以NULL填充
      mysql> select * from student left join department
      -> on dept_id= d_id;
    • 右外連接: (以右表為基準(zhǔn))
      對兩張表做連接的時候,在連接條件不匹配的時候
      留下右表中的數(shù)據(jù),而左表中的數(shù)據(jù)以NULL填充
      mysql> select * from student right join department
      -> on dept_id= d_id;

作業(yè)
從課堂上演示的students表里面
統(tǒng)計出所有人數(shù)
統(tǒng)計出age大于18的人數(shù)
統(tǒng)計出學(xué)python的人數(shù)
統(tǒng)計出學(xué)java的age大于18的人數(shù)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容