10個MySQL select語句的簡單用法範例
這篇文章主要講述的是MySQL SELECT句法的簡單分析,我們大家都知道MySQL資料庫是我們大家常使用的資料庫,其相關的應用也是備受關注的,那麼以下的文章主要是對MySQL SELECT句法的簡單分析。
1、select語句可以用回車分隔
$sql="select * from article where id=1" 和 $sql="select * from article where id=1",都可以得到正确的结果,但有时分开写或许能更明了一点,特别是当sql语句比较长时
2、批次查詢資料
可以用
in
來實作
$sql="select * from article where id in(1,3,5)"
#3、使用concat連線查詢的結果
$sql="select concat(id,"-",con) as res from article where id=1"
返回"1-article content"
4、使用locate
用法:<br/>
select locate("hello","hello baby");
存在回傳1,不存在回傳0
5、使用group by
#group by 是把相同的結果編成一組<br/>
exam:$sql="select city ,count(*) from customer group by city";
這句話的意思就是從customer表格列出所有不重複的城市,及其數量(有點類似distinct)
group by 經常與AVG(),MIN(),MAX(),SUM(),COUNT()一起使用
#6、使用having
having 允許有條件地聚合資料為群組
$sql="select city,count(*),min(birth_day) from customer group by city having count(*)>10";
這句話是先按city歸組,然後找出city地數大於10的城市
btw:使用group by + having 速度有點慢
同時having子句包含的表達式必須在之前出現過
7、組合子句
where、group by、having、order by(如果這四個都要使用的話,一般按這個順序排列)
8.使用distinct
distinct是去掉重複值用的
$sql="select distinct city from customer order by id desc";
這句話的意思就是從customer表中查詢所有的不重複的city
9、使用limit
如果要顯示某筆記錄之後的所有記錄
$sql="select * from article limit 100,-1";
10、多表查詢
$sql="select user_name from user u,member m where u.id=m.id and m.reg_date>=2015-08-13 order by u.id desc"
注意:如果user和member兩個標同時有user_name字段,會出現mysql錯誤(因為mysql不知道你到底要查詢哪個表裡的user_name),必須指明是哪個表的;
##
以上是10個MySQL select語句的簡單用法範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

如何修復 MySQL 8.4 上的 mysql_native_password 未載入錯誤
