一、查詢語句
select * from table_name;
二、查詢指定記錄
select name,population from city where id=1;
三、帶in的查詢語句
select id,name,population from city where id IN (100,101);
注意,這里的(100,101)代表的意思是數(shù)組,即100或者101的意思
四、查詢指定范圍內(nèi)的數(shù)據(jù)
select id,name,population from city where id between 10 and 20 ;
五、匹配字符的查詢
select id,name,population from city where id like "***";
六、查詢模糊字符
%=一個(gè)或多個(gè)字符串
_=一個(gè)字符串
N%d
C_ _

七、多條件查詢
where后面加上and,意思是需要滿足兩個(gè)條件;
后面加上or,意思是滿足任意一個(gè)條件即可;
and與or可以同時(shí)使用,但and的匹配優(yōu)先級(jí)高于or;
八、帶not的多條件查詢
select * from city where id<10 and district not like 'z%d';
九、查詢結(jié)果,要求不重復(fù)
select distinct countrycode from city where id<10;
十、查詢結(jié)果的排序
我們使用的order by語句
ASC代表升序,DESC代表降序
select * from city where id >10 order by popluation DESC;
十一、多列排序
select * from city where id >10 order by popluation,name DESC;
按照先后順序排序,先排populuation,然后在每個(gè)相同的內(nèi)容里面,按照用name這一列的相應(yīng)規(guī)則排序
十二、分組查詢
使用group by語句
select countrycode,count(*) as total from city where id<10 group by countrycode;
語句的意思:
統(tǒng)計(jì)城市地區(qū),與城市地區(qū)所出現(xiàn)的次數(shù),并展示出來,用group by語句將countrycode這個(gè)列里面符合條件的數(shù)據(jù)自動(dòng)分組,查詢并顯示結(jié)果
十三、對(duì)于分組之后還有條件,用having過濾分組
select countrycode,count(*) from total from city where id<101 group by countrycode having count(*)>10;
where 是在分組之前過濾數(shù)據(jù),having 是在分組之后過濾數(shù)據(jù),并且where的優(yōu)先權(quán)比having高

十四、在group by后面求和
在最后加上一個(gè)with rollup


十五、多字段分組
select * from CITY where ID<10 group by COUNTRYCODE,DIDTRICT;
即用逗號(hào)將多個(gè)字段連接在一起,并且會(huì)按從左到右的順序優(yōu)先分組
十六、限制查詢的行數(shù)
select * from CITY limit3;
select * from CITY limit 10,10;
這里表示從第10行開始,然后往后顯示10條數(shù)據(jù)