MySQL簡單查詢詳解

來源:互聯網
上載者:User

標籤:彙總函式   分組   排序   limit   條件測試   


MySQL的查詢操作

單表查詢:簡單查詢

多表查詢:串連查詢

聯集查詢



布爾條件運算式操作符

   = 等值比較   <=>:跟空值比較不會產生額外資訊的等值比較   <>:不等值   <:    <=:   >   >=   IS NULL   IS NOT NULL       LIKE:       支援的萬用字元: %(任意長度的任一字元),_(任意單個字元)   RLIKE,REGEXP: 支援使用Regex   IN: 判斷指定欄位的值是否在給定在列表中;     BETWEEN ... AND ...: 位於指定的範圍之間(x>=10 and x<=40 --> x BETWEEN 10 AND 20)



樣本

新建立一個表mysql> create table tests (sid int unsigned auto_increment not null unique key,name char(30)     not null,age tinyint unsigned not null,gender enum(‘F‘,‘M‘) not null,tutor char(30));


650) this.width=650;" src="http://s1.51cto.com/wyfs02/M00/8B/A2/wKiom1hTNDLBAEgaAAApZBokJ-s251.png-wh_500x0-wm_3-wmp_4-s_925550764.png" title="1.png" alt="wKiom1hTNDLBAEgaAAApZBokJ-s251.png-wh_50" />



添加幾個使用者mysql> insert into tests values (1,‘Guo Jing‘,27,‘M‘,‘Song Jiang‘),    (2,‘Yang Guo‘,28,‘M‘,‘Hu Sanniang‘),(3,‘Guo Polu‘,21,‘M‘,‘Jia Baoyu‘);    mysql> insert into tests values (4,‘Xue Baochai‘,‘19‘,‘F‘,‘Rong Momo‘),      (5,‘Xia Yuhe‘,37,‘F‘,‘Shi Qian‘),(6,‘Wu Yong‘,51,‘M‘,‘Lin Daiyu‘);


650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/8B/A2/wKiom1hTNMfyCL4zAAAkRowALu0842.png-wh_500x0-wm_3-wmp_4-s_2816630872.png" title="1.png" alt="wKiom1hTNMfyCL4zAAAkRowALu0842.png-wh_50" />


BETWEEN...AND...文法示範mysql> select name,age from tests where age between 25 and 40;


650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/8B/9E/wKioL1hTNQugwUMJAAAwjSSZK5A453.png-wh_500x0-wm_3-wmp_4-s_3601734811.png" title="1.png" alt="wKioL1hTNQugwUMJAAAwjSSZK5A453.png-wh_50" />


IN 文法示範mysql> select name,age from tests where age in (25,26,27,28,29);


650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/8B/A2/wKiom1hTNTaj4SA-AAArigU_LR8193.png-wh_500x0-wm_3-wmp_4-s_312419588.png" title="1.png" alt="wKiom1hTNTaj4SA-AAArigU_LR8193.png-wh_50" />


LIKE 文法示範mysql> select name from tests where name like ‘x%‘;


650) this.width=650;" src="http://s4.51cto.com/wyfs02/M00/8B/9E/wKioL1hTNWngiLSRAAAlWHYtHM8572.png-wh_500x0-wm_3-wmp_4-s_2770957590.png" title="1.png" alt="wKioL1hTNWngiLSRAAAlWHYtHM8572.png-wh_50" />


RLIKE 文法示範mysql> select name from tests where name rlike ‘^x.*‘;


650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/8B/9E/wKioL1hTNZWgwvzBAAAUnsT5KQM953.png-wh_500x0-wm_3-wmp_4-s_3545015822.png" title="1.png" alt="wKioL1hTNZWgwvzBAAAUnsT5KQM953.png-wh_50" />



//添加兩行新的資料//NULL不是字串 不需要加引號mysql> insert into tests values (7,‘tom‘,11,‘M‘,‘jerry‘),(8,‘tomy‘,13,‘M‘,NULL);


650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/8B/A2/wKiom1hTNbvDAH16AABaK5CMRbE229.png-wh_500x0-wm_3-wmp_4-s_995579512.png" title="1.png" alt="wKiom1hTNbvDAH16AABaK5CMRbE229.png-wh_50" />


IS NULLmysql> select name,tutor from tests where tutor is null;


650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/8B/A2/wKiom1hTNfiyJWd2AAAYaYvv0c4906.png-wh_500x0-wm_3-wmp_4-s_2763567198.png" title="1.png" alt="wKiom1hTNfiyJWd2AAAYaYvv0c4906.png-wh_50" />


NOT NULLmysql> select name,tutor from tests where tutor is not null;



組合條件測試

NOT, !AND, &&OR, ||


樣本


mysql> select name,gender,age from tests where age > 25 and gender=‘M‘;


650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/8B/9E/wKioL1hTNlmR8FESAABEs5MJBwY441.png-wh_500x0-wm_3-wmp_4-s_4243278516.png" title="1.png" alt="wKioL1hTNlmR8FESAABEs5MJBwY441.png-wh_50" />



排序

order by ‘排序欄位’預設為升序:ASC降序:DESC


樣本

mysql> select name,gender,age from tests where age > 25 and gender=‘M‘ order by name;mysql> select name,gender,age from tests where age > 25 and gender=‘M‘ order by name desc;


650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/8B/A2/wKiom1hTNp-Tan6qAAB-_2XPQVw799.png-wh_500x0-wm_3-wmp_4-s_1864437225.png" title="1.png" alt="wKiom1hTNp-Tan6qAAB-_2XPQVw799.png-wh_50" />



彙總函式

 SUM(), AVG(), MAX(), MIN(), COUNT()


樣本

mysql> select sum(age) from tests;mysql> select avg(age) from tests;mysql> select max(age) from tests;mysql> select count(age) from tests;


650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/8B/9E/wKioL1hTNu2D21TaAABOWsrT0Io301.png-wh_500x0-wm_3-wmp_4-s_2658315089.png" title="1.png" alt="wKioL1hTNu2D21TaAABOWsrT0Io301.png-wh_50" />


mysql> select count(*) from tests where age > 25;//年齡大於25的有4個


650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/8B/A2/wKiom1hTNxSB0us5AAAQiDn-Pbk525.png-wh_500x0-wm_3-wmp_4-s_3275084230.png" title="1.png" alt="wKiom1hTNxSB0us5AAAQiDn-Pbk525.png-wh_50" />


mysql> select sum(age) from tests where age > 25;//年齡大於25的所有年齡之和


650) this.width=650;" src="http://s4.51cto.com/wyfs02/M02/8B/9E/wKioL1hTNzaRvkPFAAARRWQNqtg946.png-wh_500x0-wm_3-wmp_4-s_2924229284.png" title="1.png" alt="wKioL1hTNzaRvkPFAAARRWQNqtg946.png-wh_50" />


分組

group by


樣本

mysql> select gender,sum(age) from tests group by gender;


650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/8B/A2/wKiom1hTN2-A_LB9AAAsH5KLCcQ659.png-wh_500x0-wm_3-wmp_4-s_1254613737.png" title="1.png" alt="wKiom1hTN2-A_LB9AAAsH5KLCcQ659.png-wh_50" />


mysql> alter table tests add classid tinyint unsigned;//為表新添加一個欄位


650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/8B/9E/wKioL1hTN5GAHik5AABm7GJgei8550.png-wh_500x0-wm_3-wmp_4-s_307075469.png" title="1.png" alt="wKioL1hTN5GAHik5AABm7GJgei8550.png-wh_50" />


插入資料mysql> update tests set classid=1 where sid=1;......


650) this.width=650;" src="http://s1.51cto.com/wyfs02/M00/8B/9E/wKioL1hTN8ajzhXgAAAsyme3mF0695.png-wh_500x0-wm_3-wmp_4-s_3672451127.png" title="1.png" alt="wKioL1hTN8ajzhXgAAAsyme3mF0695.png-wh_50" />


mysql> select classid,count(*) from tests group by classid; 或者mysql> select classid,count(name) from tests group by classid;//按班級分類 每班有多少人


650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/8B/A2/wKiom1hTN-WS7XdHAAAjIIeeers342.png-wh_500x0-wm_3-wmp_4-s_3617116206.png" title="1.png" alt="wKiom1hTN-WS7XdHAAAjIIeeers342.png-wh_50" />


mysql> select classid,count(name),sum(age) from tests group by classid;每個班的人的年齡之和



對分組(group by)的條件過濾

having    注意:使用having和不是使用where


樣本

mysql> select classid,count(*) from tests group by classid having count(*) >= 2;


650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/8B/9E/wKioL1hTODLwfk1vAAApEvuWx0M298.png-wh_500x0-wm_3-wmp_4-s_2540424838.png" title="1.png" alt="wKioL1hTODLwfk1vAAApEvuWx0M298.png-wh_50" />


mysql> select classid,count(*),sum(age) from tests group by classid having sum(age) <= 50;


650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/8B/A2/wKiom1hTOFPi6yAyAAAXmB8wgME791.png-wh_500x0-wm_3-wmp_4-s_612272994.png" title="1.png" alt="wKiom1hTOFPi6yAyAAAXmB8wgME791.png-wh_50" />


mysql> select classid,count(name),sum(age) from tests where classid in (1,2) group by classid havingsum(age) <= 50;//注意where過濾和having過濾 分別出現的位置



只返回有用的行

LIMIT 一個數為顯示的行數兩個數字為位移第一個數字行,顯示第二個數字


樣本

mysql> select * from tests limit 2;mysql> select * from tests limit 2,3;


650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/8B/9E/wKioL1hTOLfhtCADAABlZ1WzwZg558.png-wh_500x0-wm_3-wmp_4-s_188824642.png" title="1.png" alt="wKioL1hTOLfhtCADAABlZ1WzwZg558.png-wh_50" />



select語句

distinct   重複的只顯示一次SQL_CACHE  緩衝查詢結果SQL_NO_CACHE 不緩衝查詢結果


樣本

mysql> select age from tests order by age; mysql> select distinct age from tests order by age;

 



總結


select語句的執行流程

  from clause --> where clause --> group by --> having clause -->order by --> 

  select -->limit


常見用法

selectfromorder by


selectfromgroup byhaving


selectfromwhere


select -->調用內建函式


selectfromwheregroup bylimit





本文出自 “似水流年” 部落格,請務必保留此出處http://sixijie123.blog.51cto.com/11880770/1883224

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.