總網頁瀏覽量

2013年1月15日 星期二

Select基本用法

本語法以MSSQL為主
select 基本功能:從資料表單截取使用者想要看的資料
一、基本語法
Select 想要的欄位(描述想要哪些欄位colunm資料) from 那個資料表單 where 篩選條件(描述哪些列row的資料)

例如: select * from studetnt_table where ID like "M99%"
從學生資料表單調出學號碼M99的學生資料

二、排序資料:order by
order by [欄位1,欄位2][Desc Asc]
Desc 由大到小(descend),英文字母由Z到A
Asc 由小到大(ascend),英文字母由A到Z
例如 order by Student_ID,height Desc
先以學號由大到小排序後接著再以身高由大到小排序


三、篩選條件:where子句
where 用法:
1.比較型:比較大小或比較字串。
比方抓出學號大於一百的學生資料
select studentID from student_table where StudentID>100
或者抓出住在台北的學生資料
select * from student_table where city ='Taipei'
//如果不加上單引號,會誤以為Taipei是欄位

2.樣式比較型:抓出某欄位含有XX字串的資料。
where 欄位 like '字串%'
例如:抓出姓曹的學生資料
select * from student_table where name like '曹%'
where 欄位 like '%字串%'
例如:抓出地址中住在中正路的學生資料
select * from student_table where address like'%中正路%'

例如:抓出名稱A或B開頭的顧客資料
select * from customers where customerid like '[AB]%';

例如:抓出名稱為A到D開頭的顧客資料
select * form customers where customerid like '[A-D]%'


3.區間型:指定一欄位,抓取某範圍的學生資料。
where 欄位 between 數字 and 數字2
例如:抓取身高介於165到175公分學生資料
select * from student_table where height between 165 and 175
(上述語法會抓出含身高165、175的學生資料)

4.列舉型:指定一欄位,列出多個項目,抓取欄位符合項目的學生資料
where 欄位 in (''值1,'值2','值3')
例如:抓出住在台北與台中的學生資料
select * from student_table where city in ('taipei','taichung')

抓取欄位值為null的資料
select * from student_table where tele is null
(null表示不知道的概念所以用is,用=表示指定一個value給DBsystem)



四、分組group by:
針對欄位值的種類,個別作函式運算:select 函式(欄位1) from table_name group by 欄位1[,欄位2];
例如:我想知道來自各縣市的學生有多少人
select COUNT(country) from students group by Country;


切記:select子句欄位列表必須是函數或者是group by後面的欄位

五、彙總函數篩選條件:having
我們常常會找平均大於某某數的資料,利用彙總函數avg()求算之後無法用"where function(欄位)>數值"來過濾。
彙總函數的條件篩選不能用where來達成。

因此必須用having function(欄位)>數值,過濾資料
不可寫在group by 之前
having常常跟彙總函數、group by其中之一或兩者搭配



欄位出現在group by後面,才可以出現在having 後面
select ProductName from Products group by ProductName having productname like 'A%'    ;

六、select一些眉角:



1.對於某一欄位,列出所有的值域(項目):distinct
例如我們想知道學生來自哪些城市
select distinct city from student_table
(上面語法會列出學生居住的城市,我們可以知道學生來自哪些城市)
(也可以用group by,select city from student_table group by city)


2.字串串接:字串相加之後另闢欄位名稱
select firstname,lastname,'identification number:' + convert(varchar(2),emplyeeid) as ID from employees

3.欄位之間可以運算後select成新欄位。
select (AwayScore+ HomeScore)as total,* from Game where (AwayScore+HomeScore)>10
注意的事情:指令運算期間必須參考原始屬性欄位名稱,例如以下指令:
select(AwayScore+ HomeScore)as total,* from Game where total >10
M$SQL表示....:無效的資料行名稱 'total'。

4.變數型態轉換:convert(資料型態,欄位)
以下例子將整數型態轉成money型態,select出的值有小數點
select (AwayScore/CONVERT(money,homescore)) as total,* from Game
where HomeScore > 0 and (AwayScore/HomeScore) > 10

亦或者在欄位後面*1.0也可以,但是最好不要用這個方式

5.
5.1
只列出前幾項的紀錄:top 數字
例如:列出訂購量前五大的訂單明細
先列出所有訂單明細後,以訂購量由大到小排列,最後打上top 5
select top 5  * from [Order Details] order by Quantity  desc

5.2
如果有同樣訂購量情況
那麼在top 5後面加上with ties就會列出所有第五多的訂單
稱之為回傳增列跟邊界值相同的紀錄
select top 5 with ties * from [order details] order by quantity desc

5.3
列出前n百分比的紀錄select top n percent * from table order by field
例如:列出訂購量前百分之十二的訂單明細
select  top 12 percent with ties * from [Order Details] order by Quantity;





沒有留言:

張貼留言