程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> 10個mysql中select語句的簡略用法

10個mysql中select語句的簡略用法

編輯:MySQL綜合教程

10個mysql中select語句的簡略用法。本站提示廣大學習愛好者:(10個mysql中select語句的簡略用法)文章只能為提供參考,不一定能成為您想要的結果。以下是10個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

用法:
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()一路應用

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>=2006-12-28
order by u.id desc"

留意:假如user和member兩個標同時有user_name字段,會湧現mysql毛病(由於mysql不曉得你究竟要查詢哪一個內外的user_name),必需指明是哪一個表的;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved