商務英語計算機英語

數據庫中select的用法

本文已影響 3.16K人 

數據庫中select的用法的用法你知道嗎?下面小編就跟你們詳細介紹下數據庫中select的用法的用法,希望對你們有用。

padding-bottom: 66.41%;">數據庫中select的用法

  數據庫中select的用法的用法如下:

1、select語句可以用回車分隔

?

1

2

3

4

$sql="select * from article where id=1"

$sql="select * from article

where id=1",都可以得到正確的結果,但有時分開寫或許能更明瞭一點,特別是當sql語句比較長時

2、批量查詢數據

?

1

2

可以用in來實現

$sql="select * from article where id in(1,3,5)"

3、使用concat連接查詢的結果

?

1

$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 是把相同的結果編爲一組

?

1

exam:$sql="select city ,count(*) from customer group by city";

這句話的意思就是從customer表裏列出所有不重複的城市,及其數量(有點類似distinct)

group by 經常與AVG(),MIN(),MAX(),SUM(),COUNT()一起使用

6、使用having

having 允許有條件地聚合數據爲組

?

1

2

$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是去掉重複值用的

?

1

$sql="select distinct city from customer order by id desc";

這句話的意思就是從customer表中查詢所有的不重複的city

9、使用limit

如果要顯示某條記錄之後的所有記錄

?

1

$sql="select * from article limit 100,-1";

10、多表查詢

?

1

2

3

4

$sql="select user_name from user u,member m

where = and

_date>=2006-12-28

order by desc"

猜你喜歡

熱點閱讀

最新文章

推薦閱讀