需要執行SQL語句
create table student( id number(1,0) constraint studnet_id primary key, name varchar2(8), sex char(2) default '男' , age number(3) default 0 ) insert into student values ('1','王明','男',18); insert into student values ('2','孫麗','女',17); insert into student values ('3','王明','男',27); insert into student (id,sex,age) values ('4','男',27); commit;
SQL函數概述
數據庫中的函數與Java中的函數類似,都是完成一定功能的代碼的集合。根據函數操作數據行數可將SQL函數分為單行函數和多行函數:
單行函數僅對單條數據中的列進行操作並且返回一個結果,例如:select length('lanqiao') from dual——獲取字符串字符個數
多行函數可以操作成組的多條數據,每組返回一個結果,所以多行函數又稱之為組函數,例如:
select sex,count(id) from student group by sex——獲取student表中男女人數
單行函數
單行函數根據操作對象的不同分為字符函數、數字函數、轉換函數、日期函數和通用函數。
1、字符函數
a、lower(input):將大寫字符轉換為小寫,例如:select lower('AbC') from dual——> abc
b、upper(input):將小寫字符轉換為大寫,例如:select upper('AbC') from dual——>ABC
c、initcap(input):將每個單詞首字母大寫,例如:select initcap('i love you') from dual——> I Love You
d、concat(input1,input2):連接兩個字符串,等價於連接運算符||,例如:select concat('lan','qiao') from dual——> lanqiao
e、substr(input,m[,n]):從m(m>=1)位置開始截取字符串,如果n被忽略則取到字符串結尾,否則取n個字符長度,例如:
select substr('OracleDB',1) from dual——>OracleDB select substr('OracleDB',1,6) from dual——>Oracle
f、lengthc(input):獲取以字符為單位的字符串長度,例如:select lengthc('lan橋') from dual——> 4
g、lengthb(input):獲取以字節為單位的字符串長度,例如:select lengthb('lan橋') from dual——> 5(注:一個漢字2字節)
h、length(input):獲取字符串的字符數,等效於lengthc,例如:select length('lan橋') from dual——> 4
i、replace(input,char1,char2):將字符串中的char1字符串替換為char2,例如:select replace('AbAcA','A','a') from dual——> abaca
j、lpad(input,n,char):使用給定字符串從input左邊進行填充以使舊字符串的長度達到n,例如:select lpad('Oracle',9,'*') from dual——> ***Oracle
k、rpad(input,n,char):使用給定字符串從input右邊進行填充以使舊字符串的長度達到n,例如:select rpad('Oracle',9,'**') from dual——> Oracle***
l、instr(input,char[,m][,n]) :獲取char字符串在input字符串中的位置,m用於指定查找的開始位置,n用於指定char字符串第n次出現。m與n的默認值為1,即從input字符串開頭開始查找,獲取char字符串第一次出現的位置,例如:
select instr('DBOracleDB','DB') from dual——>1(從DBOracleDB字符串開頭查找DB第一次出現的位置)
select instr('DBOracleDB','DB',3) from dual——>9(從DBOracleDB字符串第3個字符查找DB第一次出現的位置)
select instr('DBOracleDB','DB',1,2) from dual——>9(從DBOracleDB字符串第1個字符查找DB第二次出現的位置)
2、數字函數
a、round(input[,n]):將數值四舍五入,參數n表示有效小數位,如果忽略則無小數位部分,例如:
select round(1.945) from dual——2 select round(1.945,2) from dual——1.95
b、trunc(input[,n]):將數值截斷,參數n表示有效小數位,如果忽略則無小數位部分,例如:
select trunc(1.945) from dual——1 select trunc(1.945,2) from dual——1.94
c、mod(m,n):返回m除以n的余數,將m與n中間逗號理解成Java中%即可,例如:select mod(1,3) from dual——1
3、轉換函數
a、to_number(char[,'fmt']):將字符串類型的數據轉換成數字類型的數據,
b、to_char(input[,'fmt']):將日期或數字類型的數據轉換為字符串
將日期類型的數據轉換為字符串:
日期格式元素
| 格式元素 |
說明 |
結果 |
| CC |
世紀 |
21 |
| 年 |
2017 |
|
| YYY、YY、Y |
年的最后3、2、1個數字 |
017、17、7 |
| Y,YYY |
年,在指定位置帶逗號 |
2,017 |
| YEAR |
拼寫年 |
TWENTY SEVENTEEN |
| BC、AD |
根據日期自動設置為BC、AD |
AD |
| B.C.、A.D. |
根據日期自動設置為B.C.、A.D. |
A.D. |
| Q |
季度 |
3 |
| MM |
月:兩位數字值 |
06 |
| MONTH |
月份的全拼,右端補齊空格,總長度為9個字符 |
JUNE |
| Mon |
月份的前3個字母,首字母大寫 |
Jun |

時間格式元素

e.g
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual
fm、th和sp的使用
fm:
i、刪除填補的空格,例如:select lengthb(to_char(sysdate,'fmmonth')) from dual
ii、刪除前導0,例如:select to_char(sysdate,'fmmm') from dual
th:表示序數,例如:select to_char(sysdate,'Qth') from dual
sp:將數字變為英文,例如:select to_char(sysdate,'Qsp') from dual
注意:為了達到更好的輸出效果,可以在格式模板中直接使用標點符號作為各元素之間的間隔,還可以在格式模板中增加字符串,但是該字符串需要用雙引號引起來,例如
select to_char(sysdate,'cc"世紀" yyyy-mm-dd hh24:mi:ss') from dual;
將數字類型的數據轉換為字符串:
c、to_date(char[,'fmt']):將字符類型轉為日期類型
fx:用於限制所傳入的日期字符串必須和格式模板精確匹配(包括標點符號和空格)
select to_date('09 03,2006','fxMM DD,YYYY') from dual;--去掉fx才可以執行
select to_date('9 3,2006','fxMM DD,YYYY') from dual;-- 去掉fx才可以執行
select to_date('09 03,2006','fxMM DD;YYYY') from dual;--可以執行
4、日期函數
a、months_between(date1,date2):返回兩個日期之間的月數,例如
select months_between(to_date('2017-3-21','yyyy-mm-dd'), to_date('2017-1-21','yyyy-mm-dd')) from dual——2
5、通用函數
a、nvl(expr,value):如果expr為空,則返回value的值;否則返回expr
b、nvl2(expr,value1,value2):如果expr1為非空,則返回value1的值,否則返回value2的值;
許小力問題:
select nvl(sysdate,1)from dual
select nvl('1',sysdate)from dual
select nvl('aa',1)from dual
select nvl2('aa',1,sysdate)from dual
c、coalesce(expr1, expr2,.. exprn):如果expr1為非空,則返回expr1的值;如果expr1為空,則返回expr2的值,依次類推,如果前面的表達式都為空,則返回exprn的值。
6、其它
a、decode (條件,值1,返回值1,值2,返回值2,...值n,返回值n,默認值)
create table user_info( name varchar2(8), sex number(1) ) insert into user_info(name,sex) values ('張三',1); insert into user_info(name,sex) values ('李四',0); insert into user_info(name,sex) values ('王五',2); commit; select name,decode(sex,0,'女',1,'男','其它') sex_name from user_info

多行函數
多行函數又稱組函數,這類函數用於對多行數據進行操作,在使用時需要注意一下幾點:
1、組函數忽略空值——可以通過nvl、nvl2或coalesce函數用一個值代替空值;
2、組函數默認考慮重復值——可以通過distinct關鍵字使組函數不考慮重復值;
常用組函數:
avg(input):求平均值,例如:select avg(age) from student——計算學生平均年齡,包括重復的年齡
max(input):求最大值,例如:select max(age) from student——獲取學生表中最大年齡
min(input):求最小值,例如:select min(age) from student——獲取學生表中最小年齡
sum(input):求和,例如:select sum(age) from student——計算學生表中年齡之和
count(*|input):求行數,如果使用*則不會忽略空值的行,例如:
select count(name) from student——3,student表中有一條數據中的name為空
select count(distinct name) from student——2,student表中有一條數據中的name為空,有兩條數據name的值重復
select count(nvl(name,' ')) from student——4, 將name為空的值置為1個空格
select count(*) from student——4,*不會忽略空值的行
stddev(input):求標准差
variance(input):求方差
注意:count、max和min組函數操作的數據的數據類型可以是char、varchar2、number或date,但不能為clob;avg、sum、stddev和varlance僅能用在數字類型的數據上。
group by:
group by用於將表中數據划分為若干個組,group by后面用於指定分組的依據,例如:
select sex,count(id) from student group by sex——將student表學生按照sex分組,然后統計每組中的人數
注意:
a、只有對應列相同的多行數據才會歸為一組,如下:
create table fruit( name varchar2(4), address varchar2(12), type_name varchar2(6) ) insert into fruit values ('香蕉','廣西','大香蕉'); insert into fruit values ('蘋果','山東','紅富士'); insert into fruit values ('香蕉','菲律賓','小香蕉');--帝王蕉比較小 insert into fruit values ('蘋果','山西','青蘋果'); insert into fruit values ('蘋果','山西','國光');
執行上面SQL語句后表中數據如下:

i、執行select name from fruit group by name SQL語句,其查詢結果如下:

ii、執行select name,address from fruit group by name,address SQL語句,其查詢結果如下:

b、如果select語句中使用group by進行了分組,則select子句中只可以有組函數和分組字段,不能含有其他字段,否則SQL語句報錯;
c、如果group by子句后面跟着order by子句,則order by子句用於排序的字段必須是組函數或分組字段;
d、如果select語句中含有where、order by,那么group by需放在where之后order by之前,即先獲取符合where條件的“有效”數據,再依據group by對數據進行分組,最后再排序;
having:
where后面不能使用多行函數,只能使用單行函數和字段,having關鍵字彌補了這一不足:having子句用於對分組結果進行約束,例如:
select name from student group by name having count(name)>1——查詢哪些名字重名了
注意:
a、having子句必須和group by子句一起使用,否則出現如下錯誤,例如:
select name from student having count(name)>1

b、having子句必須放在group by子句之后,order by子句之前
