Oracle查询
1.單表查詢
1.精確查詢
查詢語句: select * from T_OWNERS where watermeter = '1';select id,name from T_OWNERS where watermeter = '1';2.模糊查詢
select * from t_owners where name like '%xi%'3.And運算符
select * from t_owners where name like '%tommy%' and housenumber like '%5%';4.or運算符
select * from t_owners where name like '%tommy%' or housenumber like '%5%';5.or 和And混合使用
因為And的優(yōu)先級比or大,所以我們需要用()來改變優(yōu)先級 select * from t_owners where (Name like '%xi%' and housenumber like '%5%') or addressid = 3;6.范圍查詢
select * from T_Account where usernum >= 10000 and usernum <= 20000;我們也可以用between And 來實現(xiàn) select * from T_Account where usernum between 10000 And 20000;7.空值查詢
select * from T_ACCOUNT where maxnum is nullselect * from T_ACCOUNT where maxnum is not null8.去掉重復(fù)查詢
select DISTINCT addressid from t_account;9.排序查詢
升序: select * from t_account order by usenum;降序: select * from t_account order by usenum desc;10.基于偽列的查詢
在Oracle的表的使用的過程中,實際表中還有一些附加的列,成為偽列,偽列就像表中的列一樣,但是在表中并不存儲。偽列只能查詢,不能進行增刪改操作。關(guān)鍵字:ROWID和ROWNUM ROWID:表中的每一行在數(shù)據(jù)文件中都有一個物理地址,ROWID偽列返回的就是改行的物理地址,使用ROWID可以快速的定位表中的某一行。ROWID的值可以唯一的標(biāo)識表中的一行,由于ROWID返回的是該行的物理地址,因此在使用ROWID可以顯示行是如何存儲的。select rowID,t.* from t_Area t;可以通過指定的ROWID來查詢記錄 select rowID,t.* from T_AREA t where ROWID='';ROWNUM:在查詢結(jié)果集中,ROWNUM為結(jié)果集中每一行標(biāo)識一個行號,第一行返回1,第二行返回2,以此類推,通過ROWNUM偽列可以限制查詢結(jié)果集中返回的行數(shù) select rownum,t.* from T_OWNERTYPE t;11.聚合查詢
--求和 select sum(usernum) from t_account where year=‘2012’;--求平均 select avg(usernum) from t_account where year=‘2012’;--求最大 select max(usernum) from t_account where year=‘2012’; --求最小 select min(usernum) from t_account where year=‘2012’;--求統(tǒng)計個數(shù) select count(*) from t_account where year=‘2012’;--分組聚合:select 后一定是分組聚合的條件或者是聚合函數(shù) select sum(money) from t_account group by areaid; select year,areaid,sum(money) from t_account group by areaid;13.分組后條件查詢
select areaid,sum(money) from t_account group by areaid having sum(money)> 10000;2.連接查詢
1.多表內(nèi)連接查詢
select o.id idnumber,o.name name,ot.name type from T_OWNERS o, T_OWNERTYPE ot where o.ownertypeid = ot.id;2.左外連接
select ow.id,ow.name,year,month,money from t_owners ow,t_account ac where ow.id = ac.owneruuid--SQL1999規(guī)范select ow.id,ow.name,year,month,money from t_owners ow left join t_account ac on ow.id = ac.owneruuid--Oracle的語法 select ow.id,ow.name,year,month,money from t_owners ow,t_account ac where ow.id = ac.owneruuid(+)2.右外連接
select ow.id,ow.name,year,month,money from t_owners ow,t_account ac where ow.id = ac.owneruuid--SQL1999規(guī)范select ow.id,ow.name,year,month,money from t_owners ow right join t_account ac on ow.id = ac.owneruuid--Oracle的語法 select ow.id,ow.name,year,month,money from t_owners ow,t_account ac where ow.id(+) = ac.owneruuid3.子查詢:嵌套查詢
1.子查詢:where 子句中的子查詢
--單行子查詢 只返回一條記錄、單行操作符 select * from T_Account where year='2012' and month='01' and usernum > (select avg(usenum) from T_ACCOUNT where year = '2012' and month='01')--多行子查詢 IN : 等于列表中的任何一個 ANY : 和子查詢返回的任意一個值比較 ALL : 和子查詢返回的所有值比較--IN運算符 select * from T_OWNERS where addressid in(1,3,4)select id from t_address where name like ‘%花園%’ select * from t_owners where addressid in (1,4)=》 select * from t_owners where addressid in (select id from t_address where name like ‘%花園%’)不包含=》 select * from t_owners where addressid not in (select id from t_address where name like ‘%花園%’)2.子查詢:from子句中的子查詢
--from子句的子查詢是多行子查詢 select * from (select o.id idnumber,o.name name,ot.name type from T_Owners o, T_OWNERTYPE ot where o.ownertypeid = ot.id) where type = '居民'3.子查詢:select子句中的子查詢:單行語句查詢
select id,name,addressid from t_ownersselect id,name,(select name from t_address where id = addressid)addressname from t_ownersselect id,name,(select name from t_address where id = addressid)addressname (select (select name from t_area where id = areaid )from t_address where id = address)areaid from t_owners4.分頁查詢
1.簡單分頁
分頁查詢臺帳表T_Account,每頁10條記錄: 我們在Oracle進行分頁查詢,需要用到偽列ROWNUM和嵌套查詢。select rownum, t.* from t_Account t where rownum<=10select * from (select rownum, t.* from t_Account t) where r <= 20 and r > 10--基于排序的分頁 select * from t_account order by usenum desc;select * from (select rownum, t.* from t_Account t) where r <= 20 and r > 10select * from (select rownum r, t.* from (select * from t_account t order by usenum desc)t ) where r <= 20 and r > 10select * from(select rownum r, t.*) from(select * from T_ACCOUNT order by usenum desc) t where rownum<=20) where r>105.單行函數(shù)
5.1 字符函數(shù)
ASCII : 返回對應(yīng)的字符的十進制值。 CHR : 給出十進制返回字符 CONCAT : 拼接兩個字符串,與II相同。 INITCAT : 將字符串的第一個字母變成為大寫。 INSTR : 找出某個字符串的位置。 INSTRB : 找出某個字符串的位置和字節(jié)數(shù) LENGTH : 以字符給出字符串的長度。 LENGTHB : 以字節(jié)給出字符串的長度。 LOWER : 將字符串換成小寫。 LPAD : 使用指定的字符在字符的左邊填充。 LTRIM : 在左邊裁剪掉制定的字符。 RPAD : 使用指定的字符在字符的右邊填充。 RTRIM : 在右邊裁剪掉指定的字符。 REPLACE : 執(zhí)行字符串的搜索和替換。 SUBSTR : 取字符串的子串。 SUBSTRB : 取字符串的子串以字節(jié)的形式。 SOUNDEX : 返回一個同音字符串。 TRANSLATE : 執(zhí)行字符串搜索和替換。 TRIM : 裁剪掉前面或后面的字符串。 UPPER : 將字符串變?yōu)榇髮憽?-常用字符函數(shù) ---求長度,dual是偽表 select length('ABCD') from dual;---求字符串的子串SUBSTR select substr('ABCD',2,2) from dual;---字符串拼接 select concat('ABC','D') from dual; select concat(concat('ABC','D'),'EF') from dual;---'||'用于拼接 select 'ABC' || 'D' from dual;5.2數(shù)值函數(shù)
ABS() : 絕對值; CEIL() : 大于或等于 COS() : 余弦 COSH(): 反余弦 EXP():e的次冪 FLOOR():小于或等于的最大整數(shù) LN():自然對數(shù) LOG():以10 為底的對數(shù) MOD(value,divisor)求模 POWER(value,exponent)value的exponent次冪 ROUND(value,precision)按precision精度四舍五入 SIGN(value)value為正返回1,為負(fù)返回-1,為0返回0; SIN:余弦 SINH:反余弦 SQRT:平方根 TAN:正切 TANH:反正切 TRUNC(value,按precision)按照precision截取value VSIZE(value):返回value在ORACLE的--數(shù)值函數(shù) select round(100.567) from dual; select round(100.567,2) from dual; --數(shù)字截取 select trunc(100.456) from dual;--取模:余數(shù) select mod(10,3) from dual;5.3日期函數(shù)
--加月函數(shù) select add_months(sysdate,-2) from dual; --求最后一天 select last_day(sysdate) from dual; select last_day(sysdate-4) from dual;--日期截取 select trunc(sysdate) from dual; --按日截取,把時間去掉; select trunc(sysdate,'mm') from dual;--按月截取,把日截掉 select trunc(sysdate,'yyyy') from dual;--按年截取 select trunc(sysdate,'hh') from dual;--按時截取 select trunc(sysdate,'mi') from dual;--按分截取5.4轉(zhuǎn)換函數(shù)
--數(shù)字轉(zhuǎn)成字符串 select TO_ChAR(100) || 'fen' from dual;--日期轉(zhuǎn)字符串 select to_char(sysdate,'yyyy-mm-dd') from dual; select to_char(sysdate,'yyyy')||‘年’ || tochar(sysdate,'mm')||‘月’ from dual; select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;--字符串轉(zhuǎn)日期 select to_date('2016-03-10','yyyy-mm-dd') from dual;--字符串轉(zhuǎn)數(shù)字 select to_number('100') + 10 from dual;5.4其他函數(shù)
--空值處理函數(shù) NVL :檢測的值,如果為null值 select NVL(null,0) from dual; select NVL2(null,0,100) from dual; select nvl2(maxnum,to_char(maxnum),'不限') from t_pricetable.--條件取值 decode select decode(100,1,2,3,4,100,200) from dual; 參數(shù)個數(shù)是奇數(shù),就是會缺省。select name,decode(ownertypeid,1,'people',2,'administrator') from t_owner;select name,(case ownertypeid when 1 then 'citizen' when 2 then 'administrator' when 3 then 'customer' end) from t_owners;select name,(case when ownertypeid = 1 then 'citizen' when ownertypeid = 2 then 'administrator' when ownertypeid = 3 then 'customer' end) from t_owners;6.行列轉(zhuǎn)換
select (select name from t_area where id=areaid) area,sum(case when month='01' then money else 0 end) January from t_account where year = '2012' group by areaid;7.分析函數(shù)
--值相同,排名相同,序號跳躍 select rank() over( order by usenum desc),t.* from t_account t;--值相同,排名相同,序號跳躍 select dense_rank() over( order by usenum desc),t.* from t_account t;--序號連續(xù),不管值是否相同 select row_number() over( order by usenum desc),t.* from t_account t;--用分析函數(shù)實現(xiàn)分頁 select * from (select row_number() over(order by usenum desc),t.* from t_account t) where rownumber<=20 and rownumber>10;8.集合運算
--并集 union all (包括重復(fù)內(nèi)容) select * from t_owners where id>5 union all select * from t_owners where id<8;--并集(去掉重復(fù)內(nèi)容) select * from t_owners where id>5 union select * from t_owners where id<8;--交集,兩個集合重復(fù)的部分 select * from t_owners where id>5 intersect select * from t_owners where id<8;--差集 select * from t_owners where id>5 minus select * from t_owners where id<8;--差集分頁 select rownum , t.* from t_account t where rownum <= 20 minus select rownum, t.* from t_account t where rownum <= 10;9.綜合查詢
10.總結(jié)
總結(jié)
- 上一篇: [html] 你觉得写Html难吗?难
- 下一篇: [css] 说下你对backgroun