DBA SQL基礎(chǔ)應(yīng)用

1檩小、select

1.1 作用
獲取mysql中的數(shù)據(jù)行
1.2 單獨(dú)使用select
1) select @@xxxx; 獲取參數(shù)信息
      select @@prot;
      down variables like '%innodb%'
2) select 函數(shù)()
      select database();
      select now();
      select version();
1.3 SQL92標(biāo)準(zhǔn)的使用語法
1) select語法執(zhí)行順序(單表)
      select開始--->from子句--->where子句--->group by子句(select之后的執(zhí)行條件)--->having子句--->order by--->limit
2) select 語句應(yīng)用
      1)from
         例子:查詢city表中的所有數(shù)據(jù)
               use world;
               select * from city;  --->適合表數(shù)據(jù)行較少的验毡,生產(chǎn)中使用較少的
               select * from world city;
         例子:查詢name和population所有值
               select name,population from city;
               select name,population from world city;
         例子:?jiǎn)伪聿樵兙毩?xí)環(huán)境,world數(shù)據(jù)庫(kù)下的介紹
               show tables from world;
               city(城市):
               --------------------
               desc city;
               id:自增的無關(guān)列卦碾,數(shù)據(jù)行的需要
               name:城市名字
               countrycode:城市所在的國(guó)家代號(hào)图云,CHN,USA,JPN...
               district:城市的所在區(qū)域,中國(guó)是省的意思卤材,美國(guó)是洲的意思
               population:城市的人口數(shù)量
               說明:此表示歷史數(shù)據(jù)遮斥。
               country(國(guó)家)
               countrylanguage(國(guó)家語言)
               --------------------
               剛?cè)肼毜腄BA熟悉業(yè)務(wù):
               DBA剛?cè)肼殨r(shí),DBA的任務(wù)
               1扇丛、搞清楚架構(gòu)
                  通過公司架構(gòu)圖术吗,搞清楚數(shù)據(jù)庫(kù)的物理架構(gòu)
                  1-2天
                  邏輯結(jié)構(gòu):
                  1)生產(chǎn)庫(kù)信息(容易達(dá)到)
                  2)庫(kù)下表信息(非常復(fù)雜)
                     a、開發(fā)和業(yè)務(wù)人員打好關(guān)系
                     b帆精、搞到ER圖(PD)
                     c较屿、啥都沒有咋整?
                        1)卓练、找到建表語句隘蝎,如果有注釋,讀懂注釋就可以了襟企。如果沒有注釋嘱么,目前只能根據(jù)列名翻譯
                        2)、找到表中部分?jǐn)?shù)據(jù)顽悼,分析數(shù)據(jù)特點(diǎn)曼振,達(dá)到了解列功能的一個(gè)目的
      2)where
               where 配合 等值查詢
               查詢中國(guó)的城市信息
               select * from world.city where countrycode='CHN';
               查詢美國(guó)的城市信息
               select * from world.city where countrycode='USA';
               where 配合 不等值 (> < >= <= <>)
               查詢世界上人口小于100的城市
               select * from world.city where population<100;
               查詢世界上人口大于10000000城市
               select * from world.city where Population>10000000;
               --------------------------
               where 配合 模糊(like)
               查詢國(guó)家代號(hào)是C開頭的城市
               select * from world.city where countrycode like 'C%';
               注意:like語句在MySQL中几迄,不要出現(xiàn)%在前面的清空。因此效率很低冰评,不走索引
               --------------------------
               where 配合 邏輯連接符(AND OR)
               查詢城市人口在10000和20000之間的
               select * from world.city where population > 10000 and population < 20000;
               查詢中國(guó)或美國(guó)的的城市信息
               select * from world.city where countrycode='CHN' or countrycode='USA'
               用union all來實(shí)現(xiàn)
               select * from world.city where countrycode='CHN' union all select * from world.city where countrycode='USA';
      3)group by
               group by 配合聚合函數(shù)應(yīng)用
                 常用的聚合函數(shù):
                   avg () 平均數(shù)
                   count () 統(tǒng)計(jì)個(gè)數(shù)
                   sum () 統(tǒng)計(jì)總數(shù)
                   max () 最大數(shù)
                   min () 最小數(shù)
                   group_concat () 統(tǒng)計(jì)列表
               -------------------------
               統(tǒng)計(jì)每個(gè)國(guó)家的總?cè)丝跀?shù)
               select countrycode,sum(population) from world.city group by countrycode;
               統(tǒng)計(jì)每個(gè)國(guó)家的城市個(gè)數(shù)
               select countrycode,count(id) from world.city group by countryCode;
               統(tǒng)計(jì)每個(gè)國(guó)家的省的個(gè)數(shù)
               select countrycode,group_concat(district) from world.city group by countrycode;
               統(tǒng)計(jì)中國(guó)每個(gè)省的城市名列表
               select district,group_concat(name) from world.city where countrycode='CHN' group by district;
               統(tǒng)計(jì)中國(guó)每個(gè)省的總?cè)藬?shù)
               SELECT district, sum(population) FROM world.city WHERE countrycode = 'CHN' GROUP BY district;
      4)select
      5)having  和Linux中的管道一樣映胁。
               統(tǒng)計(jì)中國(guó),每個(gè)省的總?cè)丝诖笥?0000000萬的人數(shù)
               SELECT district, sum(population) FROM world.city WHERE countrycode = 'CHN' GROUP BY district having sum(population) >10000000;
      6)order by 后面加desc是從大到小集索,不加則從小到大屿愚。
               統(tǒng)計(jì)中國(guó),每個(gè)省的總?cè)藬?shù)從小到大排序
               SELECT district, sum(population) FROM world.city WHERE countrycode = 'CHN' GROUP BY district order by sum(population);
               統(tǒng)計(jì)中國(guó)务荆,每個(gè)省的總?cè)藬?shù)從大到小排序
               SELECT district, sum(population) FROM world.city WHERE countrycode = 'CHN' GROUP BY district order by sum(population) desc;
               統(tǒng)計(jì)中國(guó)妆距,每個(gè)省的總?cè)藬?shù)從小到大排序
               SELECT * FROM world.city WHERE countrycode = 'CHN' order by population desc;
      7)limit m,n 跳過m行,顯示n行
         limit m offset n  跳過m行函匕,顯示n行
1.4 多表連接查詢
1) 介紹4張測(cè)試表的關(guān)系
2) 什么時(shí)候用娱据?
      需要查詢的數(shù)據(jù)是來自于多張表時(shí)。
3) 怎么去多表連接查詢
      傳統(tǒng)的連接:基于where條件
          1盅惜、找表之間的關(guān)系列
          2中剩、排列查詢條件
          需求:人口數(shù)量小于100人的城市,所在國(guó)家的國(guó)土面積(城市名抒寂,國(guó)家名结啼,國(guó)土面積)
                select name,countrycode from city where population<100;  得到PCN
                select name,surfacearea from country where code='PCN';
                上面命令合為下邊的一條:
                select city.name,country.name,country.surfacearea from city,country where city.countrycode = country.code and city.population<100;
      ----------------
      內(nèi)連接
          1、找表之間的關(guān)系列
          2屈芜、將倆表放在join左右
          3郊愧、將關(guān)聯(lián)條件了放在on后面
          4、將所有的查詢條件進(jìn)行羅列井佑。
      ---------------
      例子:
           1属铁、查詢?nèi)丝跀?shù)量小于100人的國(guó)家名。
              select country.name,city.name,country.SurfaceArea from city join country on city.countrycode=country.code where city.population<100;
           2躬翁、查詢oldguo老師和他教的課程名稱焦蘑。
              select teacher.tname,course.cname from teacher join course on teacher.tno=course.tno where teacher.tname='oldguo';
           3、查詢oldguo老師教的學(xué)生姓名列表
           select teacher.tname,group_concat(student.sname)
           from teacher
           join course
           on teacher.tno=course.tno
           join score
           on course.cno=score.cno
           join student
           on score.sno=student.sno
           where teacher.tname='oldguo';

           6盒发、查詢oldboy老師教的不及格學(xué)生的姓名
           select teacher.tname,group_concat(student.sname)
           from teacher
           join course
           on teacher.tno=course.tno
           join score
           on course.cno=score.cno
           join student
           on score.sno=student.sno
           where teacher.tname='oldguo' and score.score <60
           group by student.sname;
           7例嘱、 統(tǒng)計(jì)zhang3,學(xué)習(xí)了幾門課
           select student.sname,sum(score.sno)
           from student
           join score
           on student.sno=score.sno
           join course
           on score.cno=course.cno
           where student.sname='zhang3' and student.sno
           group by student.sno;
           8、 統(tǒng)計(jì)zhang3,學(xué)習(xí)了幾門課
           select student.sname,course.cname
           from student
           join score
           on student.sno=score.sno
           join course
           on score.cno=course.cno
           where student.sname='zhang3';
           9宁舰、查詢oldguo老師教的學(xué)生名.
           select teacher.tname,student.sname
           from teacher
           join course
           on teacher.tno=course.tno
           join score
           on course.cno=score.cno
           join student
           on score.sno=student.sno
           where teacher.tname='oldguo';
           10蝶防、查詢oldguo所教課程的平均分?jǐn)?shù)
           select teacher.tname,avg(score.score)
           from teacher
           join course
           on teacher.tno=course.tno
           join score
           on course.cno=score.cno
           where teacher.tname='oldguo';
           11.每位老師所教課程的平均分,并按平均分排序
           select teacher.tname,avg(score.score)
           from teacher
           join course
           on teacher.tno=course.tno
           join score
           on course.cno=score.cno
           group by teacher.tno
           order by avg(score.score);
           12.查詢oldguo所教的不及格的學(xué)生姓名
           select teacher.tname,group_concat(student.sname)
           from teacher
           join course
           on teacher.tno=course.tno
           join score
           on course.cno=score.cno
           join student
           on score.sno=student.sno
           where teacher.tname='oldguo' and score.score <60
           group by student.sname;
           13.查詢所有老師所教學(xué)生不及格的信息
           select teacher.tname,student.sname,score.score
           from teacher
           join course
           on teacher.tno=course.tno
           join score
           on course.cno=score.cno
           join student
           on score.sno=student.sno
           where score.score <60
           ----------------------------------
           mysql> SELECT course.cno,course.cname,SUM(sc.score)
               -> FROM course
               -> JOIN sc
               -> ON course.cno = sc.cno
               -> GROUP BY course.cname;

           ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'school.course.cno' which
           is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

           1. 在select后面出現(xiàn)的列,不是分組條件明吩,并且沒有在函數(shù)中出現(xiàn)间学。
           2. 如果group by 后是主鍵列或者是唯一條件列。如下:

           SELECT
           course.cno,course.cname,SUM(sc.score)                                                                    FROM course
           JOIN sc
           ON course.cno = sc.cno
           GROUP BY course.cno;
           -------------------------------------

2、show
3低葫、Information_schema

--- 6. 查詢oldboy老師教的不及格學(xué)生的姓名
--- 7. 統(tǒng)計(jì)zhang3,學(xué)習(xí)了幾門課
--- 8. 查詢zhang3,學(xué)習(xí)的課程名稱有哪些?
--- 9. 查詢oldguo老師教的學(xué)生名.
--- 10.查詢oldguo所教課程的平均分?jǐn)?shù)
--- 11.每位老師所教課程的平均分,并按平均分排序
--- 12.查詢oldguo所教的不及格的學(xué)生姓名
--- 13.查詢所有老師所教學(xué)生不及格的信息
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末详羡,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子嘿悬,更是在濱河造成了極大的恐慌实柠,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件善涨,死亡現(xiàn)場(chǎng)離奇詭異窒盐,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)钢拧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門蟹漓,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人源内,你說我怎么就攤上這事葡粒。” “怎么了膜钓?”我有些...
    開封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵嗽交,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我颂斜,道長(zhǎng)夫壁,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任沃疮,我火速辦了婚禮盒让,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘忿磅。我一直安慰自己糯彬,他們只是感情好凭语,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開白布葱她。 她就那樣靜靜地躺著,像睡著了一般似扔。 火紅的嫁衣襯著肌膚如雪吨些。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天炒辉,我揣著相機(jī)與錄音豪墅,去河邊找鬼。 笑死黔寇,一個(gè)胖子當(dāng)著我的面吹牛偶器,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼屏轰,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼颊郎!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起霎苗,我...
    開封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤姆吭,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后唁盏,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體内狸,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年厘擂,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了昆淡。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡驴党,死狀恐怖瘪撇,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情港庄,我是刑警寧澤倔既,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站鹏氧,受9級(jí)特大地震影響渤涌,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜把还,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一实蓬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧吊履,春花似錦安皱、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至缀踪,卻和暖如春居砖,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背驴娃。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工奏候, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人唇敞。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓蔗草,卻偏偏與公主長(zhǎng)得像咒彤,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子咒精,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容