MySQL的SELECT語句技巧

來源:互聯網
上載者:User

以下的文章主要描述的是MySQL SELECT提示大全,MySQL SELECT在實際中的應用比例還是佔為多數的,如果你對這一新開發的技術,心存好奇的話,以下的文章將會揭開它的神秘面紗。

記錄一些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

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

返回1

不存在返回0

5、使用group by

以前一直沒怎麼搞明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()一起使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.