MySQL數據庫select語句的使用方法

select語句可 以用回車分隔html

$sql="select * from article where id=1"和  $sql="select * from article where id=1"
均可以獲得正確的結果,但有時分開寫或許能 更明瞭一點,特別是當sql語句比較長時。

批量查詢數據mysql

能夠用in 來實現 
$sql="select * from article where id  ;in(1,3,5)"

使用concat鏈接查詢的結果linux

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

使用locatesql

用法: select locate("hello","hello baby");返回1
不存在返回 0

使用group byide

之前一直沒怎麼搞明group by  和 order by,其實也滿簡單的,group by 是把相同的結 果編爲一組
exam:$sql="select city ,count(*)  from customer group by city";
這句話的意 思就是從customer表裏列出全部不重複的城市,及其數量(有點相似distinct) 
group by 常常與AVG(),MIN(),MAX(),SUM(),COUNT()一塊兒使用

使用havingspa

having 容許有條件地聚合數據爲組 
$sql="select city,count(*),min(birth_day)  from customer
group by city having c ount(*)>10";
這句話是先按city歸組,而後找出city地數量大於10的城 市
btw:使用group by + having 速度有點慢
同 時having子句包含的表達式必須在以前出現過

組合子句orm

where、 group by、having、order by(若是這四個都要使用的話,通常按這 個順序排列)

使用distincthtm

distinct是去掉重複值用的 
$sql="select distinct city from customer  order by id desc";
這句話的意思就是從customer表中查 詢全部的不重複的cit

使用limitci

若是要顯示某條記錄以後的所 有記錄 
$sql="select * from article limit 100,- 1";

多表查詢get

$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字段,會出現mysql錯誤(由於mysql不知道你到 底要查詢哪一個表裏的user_name),必須指明是哪一個表的

相關文章
相關標籤/搜索