《Mysql應用10個mysql中select語句的簡單用法》要點:
本文介紹了Mysql應用10個mysql中select語句的簡單用法,希望對您有用。如果有疑問,可以聯系我們。
1、select語句可以用回車分隔MYSQL數據庫
$sql="select * from article where id=1" 和 $sql="select * from article where id=1",都可以得到正確的結果,但有時分開寫或許能更明了一點,特別是當sql語句比擬長時
2、批量查詢數據MYSQL數據庫
可以用in來實現 $sql="select * from article where id in(1,3,5)"
3、使用concat連接查詢的結果MYSQL數據庫
$sql="select concat(id,"-",con) as res from article where id=1"
返回"1-article content"MYSQL數據庫
4、使用locateMYSQL數據庫
用法:
select locate("hello","hello baby");返回1
不存在返回0MYSQL數據庫
5、使用group byMYSQL數據庫
以前一直沒怎么搞明group by 和 order by,其實也滿簡單的,group by 是把相同的結果編為一組
MYSQL數據庫
exam:$sql="select city ,count(*) from customer group by city";
這句話的意思就是從customer表里列出所有不重復的城市,及其數量(有點類似distinct)
group by 經常與AVG(),MIN(),MAX(),SUM(),COUNT()一起使用MYSQL數據庫
6、使用havingMYSQL數據庫
having 允許有條件地聚合數據為組
MYSQL數據庫
$sql="select city,count(*),min(birth_day) from customer group by city having count(*)>10";
這句話是先按city歸組,然后找出city地數量大于10的城市
btw:使用group by + having 速度有點慢
同時having子句包括的表達式必須在之前出現過MYSQL數據庫
7、組合子句MYSQL數據庫
where、group by、having、order by(如果這四個都要使用的話,一般按這個順序排列)MYSQL數據庫
8、使用distinctMYSQL數據庫
distinct是去掉重復值用的
MYSQL數據庫
$sql="select distinct city from customer order by id desc";
這句話的意思就是從customer表中查詢所有的不重復的cityMYSQL數據庫
9、使用limitMYSQL數據庫
如果要顯示某條記錄之后的所有記錄
MYSQL數據庫
$sql="select * from article limit 100,-1";
10、多表查詢MYSQL數據庫
$sql="select user_name from user u,member m where u.id=m.id and m.reg_date>=2006-12-28 order by u.id desc"
注意:如果user和member兩個標同時有user_name字段,會出現mysql錯誤(因為mysql不知道你到底要查詢哪個表里的user_name),必需指明是哪個表的;MYSQL數據庫
維易PHP培訓學院每天發布《Mysql應用10個mysql中select語句的簡單用法》等實戰技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養人才。