掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
SELECT 語句的作用是從數(shù)據(jù)庫中返回信息,本篇文章重點為大家分享一下MySQL的select語句。

成都創(chuàng)新互聯(lián)公司2013年開創(chuàng)至今,先為利川等服務(wù)建站,利川等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為利川企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
$sql="select * from article where id=1"和 $sql="select * from article where id=1"
都可以得到正確的結(jié)果,但有時分開寫或許能 更明了一點,特別是當(dāng)sql語句比較長時。
可以用in 來實現(xiàn)
$sql="select * from article where id ;in(1,3,5)"
$sql="select concat(id,"-",con) as res from article where id=1"
返回 "1-article content"
用法: select locate("hello","hello baby");返回1
不存在返回 0
以前一直沒怎么搞明group by 和 order by,其實也滿簡單的,group by 是把相同的結(jié) 果編為一組
exam:$sql="select city ,count(*) from customer group by city";
這句話的意 思就是從customer表里列出所有不重復(fù)的城市,及其數(shù)量(有點類似distinct)
group by 經(jīng)常與AVG(),MIN(),MAX(),SUM(),COUNT()一起使用
having 允許有條件地聚合數(shù)據(jù)為組
$sql="select city,count(*),min(birth_day) from customer group by city having c ount(*)>10";
這句話是先按city歸組,然后找出city地數(shù)量大于10的城 市
btw:使用group by + having 速度有點慢
同 時having子句包含的表達式必須在之前出現(xiàn)過
where、 group by、having、order by(如果這四個都要使用的話,一般按這 個順序排列)
distinct是去掉重復(fù)值用的
$sql="select distinct city from customer order by id desc";
這句話的意思就是從customer表中查 詢所有的不重復(fù)的city
如果要顯示某條記錄之后的所 有記錄
$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兩個標同時有user_name字段,會出現(xiàn)mysql錯誤(因為mysql不知道你到 底要查詢哪個表里的user_name),必須指明是哪個表的;

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流