MySQL數(shù)據(jù)庫select語句的使用方法

select語句可 以用回車分隔

$sql="select * from article where id=1"和? $sql="select * from article where id=1"

都可以得到正確的結(jié)果,但有時(shí)分開寫或許能 更明了一點(diǎn),特別是當(dāng)sql語句比較長時(shí)。

批量查詢數(shù)據(jù)

可以用in 來實(shí)現(xiàn)

$sql="select * from article where id? ;in(1,3,5)"

使用concat連接查詢的結(jié)果

$sql="select concat(id,"-",con)? as res from article where id=1"

返回 "1-article content"

使用locate

用法: select locate("hello","hello baby");返回1

不存在返回 0

使用group by

以前一直沒怎么搞明group by? 和 order by,其實(shí)也滿簡單的,group by 是把相同的結(jié) 果編為一組

exam:$sql="select city ,count(*)? from customer group by city";

這句話的意 思就是從customer表里列出所有不重復(fù)的城市,及其數(shù)量(有點(diǎn)類似distinct)

group by 經(jīng)常與AVG(),MIN(),MAX(),SUM(),COUNT()一起使用

使用having

having 允許有條件地聚合數(shù)據(jù)為組

$sql="select city,count(*),min(birth_day)? from customer

group by city having c ount(*)>10";

這句話是先按city歸組,然后找出city地?cái)?shù)量大于10的城 市

btw:使用group by + having 速度有點(diǎn)慢

同 時(shí)having子句包含的表達(dá)式必須在之前出現(xiàn)過

組合子句

where、 group by、having、order by(如果這四個(gè)都要使用的話,一般按這 個(gè)順序排列)

使用distinct

distinct是去掉重復(fù)值用的

$sql="select distinct city from customer? order by id desc";

這句話的意思就是從customer表中查 詢所有的不重復(fù)的city

使用limit

如果要顯示某條記錄之后的所 有記錄

$sql="select * from article limit 100,- 1";

多表查詢

$sql="select user_name from user? u,membe r? m

where u.id=m.id and

m.reg_date> ;=2006-12-28

order by u.id desc"

注意:如果user和 member兩個(gè)標(biāo)同時(shí)有user_name字段,會(huì)出現(xiàn)mysql錯(cuò)誤(因?yàn)?a target="_blank" rel="nofollow">mysql不知道你到 底要查詢哪個(gè)表里的user_name),必須指明是哪個(gè)表的;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容