MySQL語句測試——資料查詢

來源:互聯網
上載者:User

標籤:橫線   val   多列   資料查詢   重複   HERE   個數   計算   sde   

MySQL語句測試——資料查詢
3.4 資料查詢一、單表查詢/*1.選擇表中的若干列,各個列的先後順序和語句中列名從左至右的順序一致    select 目標運算式 from 表名;*//*(1)select 列名 from 模式名.表名;*/select Sno,Sname from zyl.student;/*(2)若要查詢全部列可用*代替*/select * from student;/*(3)查詢經過計算的值(目標運算式:列名/運算式/字串常量/函數等)*/select Sname,2014-Sage from student;select Sname,‘Year of Birth:‘,2014-Sage,lower(Sdept) from student;/*(4)指定別名來改變查詢結果的標題    select 列名/運算式/字串常量/函數 別名 from 模式名.表名;*/select Sname,‘Year of Birth:‘ birth,2014-Sage birthday,lower(Sdept) department from student;/*2.選擇表中的若干元組    select all/distinct(預設為all) 列名/運算式/字串常量/函數 from 模式名.表名;*//*(1)消除取值重複的行*/select Sdept from student;          /*此時有很多重複項*/select distinct Sdept from student; /*distinct去掉了重複項*//*(2)查詢滿足條件的元組,可以通過where語句實現*//*①比較大小=, >, <, >=, <=, !=, <>, !>, !<*/select sname,sage from student where Sage=20;/*②確定範圍,特指資料為數值型的between... and... ,not between... and...*/select sname,sdept,sage from student where sage between 19 and 20;/*③確定集合in, not in*/select sname,sdept,sage from student where sdept in(‘CS‘);/*④字元匹配like, not like%(百分比符號)表示任意長度的字串_(下橫線)表示任意單個字元*/select sname,sdept,sage from student where sname like ‘張%‘;/*所要查詢的字串本身有%或_時,不需要轉義*/insert into student(sno,sname,ssex,sage,sdept) values (‘20162300‘,‘婁_java‘,‘男‘,‘40‘,‘IS‘);select sno,sname from student where sname like ‘婁\_java‘;               /*運行成功*/select sno,sname from student where sname like ‘婁\_java‘ escape ‘\‘;    /*報錯*//*⑤涉及控制的查詢is null, is not null*/insert into student(sno,sname,ssex,sdept,sage) values (‘20160000‘,‘‘,‘‘,‘‘,‘0‘);select sno,sname,sdept,sage from student where sname is null;           /*此時並不能查到20160000*/insert into student(sno,ssex,sdept,sage) values (‘20160001‘,‘‘,‘‘,‘0‘);select sno,sname,sdept,sage from student where sname is null;           /*此時能查到20160001,顯示sname為null*//*⑥多重條件查詢and, or, not*/select sno,sname,sdept,sage from student where Sdept=‘CS‘ and Sage<20;select sname,sdept,sage from student where sdept in(‘CS‘,‘43‘);與select sname,sdept,sage from student where sdept=‘CS‘ or sdept=‘43‘;等價/*3.order by子句,asc升序、desc降序*//*4.聚集合函式count :統計元組個數count distinct/all 列名:統計一列中值的個數sum distinct/all 列名:計算一列值的總和avg distinct/all 列名:計算一列值的平均數max distinct/all 列名:求一列值的最大值min distinct/all 列名:求一列值的最小值*//*5.group by子句將查詢結果按某一列或多列的值分組,值相等的為一組select 列名/運算式/字串常量/函數 from 表名 group by 列名 having 條件條件運算式;where語句不能與group by連用,where子句中不能用聚集合函式作為條件運算式,用having代替*/二、串連查詢三、巢狀查詢四、集合查詢五、基於派生表查詢

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.