oracle中一些用法與方法區分

來源:互聯網
上載者:User

標籤:

1. case用法:

--簡單case函數case sexwhen ‘1‘ then ‘boy‘when ‘2‘ then ‘girl‘else ‘其他‘end; --case搜尋函數casewhen sex =‘1‘ then ‘boy‘when sex =‘2‘ then ‘girl‘else ‘其他‘end;

舉例:判斷工資等級,統計每個等級的人數,

SELECTCASE WHEN salary <= 500 THEN ‘1‘WHEN salary > 500 AND salary <= 600  THEN ‘2‘WHEN salary > 600 AND salary <= 800  THEN ‘3‘WHEN salary > 800 AND salary <= 1000 THEN ‘4‘ELSE NULL END salary_class,COUNT(*)FROM    Table_AGROUP BYCASE WHEN salary <= 500 THEN ‘1‘WHEN salary > 500 AND salary <= 600  THEN ‘2‘WHEN salary > 600 AND salary <= 800  THEN ‘3‘WHEN salary > 800 AND salary <= 1000 THEN ‘4‘ELSE NULL END;

 

2. sum()與count()區別:

cout()是返回匹配條件的行數。

cout(column_name)函數返回指定列的值得數目(null不計入):

select count(column_name) from table_name

cout(*)函數返回表中的記錄數:

select count(*) from table_name;---返回table表中所有的記錄(記錄也就是一條資料,即行,一條資料包含多個資料,每個資料就是一個欄位)

 

sum()函數

sum函數返回數值列的總數(總額)

select sum(coulum_name) from table_name,

例如:

select sum(orderprice) as ordertotal from orders;

功能是尋找"orderprice"該欄位的總數。--也就是該欄位下的所有值,

 

*:沒有sum(*)這種方法。

 

 

3. select into 與insert into select區別:

insert into table2 (feild1,feils2,..) select value1,value2,.. from table1;

要求目標表table2必須存在,由於目標表table2已經存在,所以除了插入源表table1的欄位外,還可以插入常量。

如:Insert into Table2(a, c, d) select a,c,5 from Table1

 

select into from :

select vae1,val2 into table2 from table1

要求目標表table2不存在,因為在插入時會自動建立表table2,並將table1中指定欄位資料複製到table2。

參考:http://www.cnblogs.com/freshman0216/archive/2008/08/15/1268316.html

 

4. order by 與group by 用法

ORDER BY 用在對查詢結果進行排序,即查詢什麼排序相應欄位

GROUP BY 用於進行分組排序,與集合函數一起用,舉例:

SELECT f_id ,SUM(f_price) AS total_price FROM fruits GROUP BY s_id;

 後面必須跟著一個 GROUP BY s_id,如果沒有,執行編譯時間會報錯:

ERROR:  column "fruits.s_id" must appear in the GROUP BY clause or be used in an aggregate function

 

oracle中一些用法與方法區分

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.