1. 程式人生 > >Oracle-day02 上

Oracle-day02 上

Oracle-day02 上

一、單表查詢
(一)簡單條件查詢
1.精確查詢
需求:查詢水表編號為 30408 的業主記錄
查詢語句:

select * from T_OWNERS where watermeter=‘30408‘
查詢結果:
技術分享圖片
2.模糊查詢
需求:查詢業主名稱包含“劉”的業主記錄
查詢語句:

select * from t_owners where name like ‘%劉%‘
查詢結果:
技術分享圖片

  1. and 運算符
    需求:查詢業主名稱包含“劉”的並且門牌號包含 5 的業主記錄
    查詢語句:
select * from t_owners where name like ‘%劉%‘ and housenumber
like ‘%5%‘

查詢結果:

技術分享圖片

  1. or 運算符
    需求:查詢業主名稱包含“劉”的或者門牌號包含 5 的業主記錄
    查詢語句:
select * from t_owners
where name like ‘%劉%‘ or housenumber like ‘%5%‘

查詢結果:
技術分享圖片

  1. and 與 or 運算符混合使用
    需求:查詢業主名稱包含“劉”的或者門牌號包含 5 的業主記錄,並且地址編號
    為 3 的記錄。
    語句:

select * from t_owners where (name like ‘%劉%‘ or housenumber
like ‘%5%‘) and addressid=3
查詢結果:
技術分享圖片
因為 and 的優先級比 or 大,所以我們需要用 ( ) 來改變優先級。

  1. 範圍查詢
    需求:查詢臺賬記錄中用水字數大於等於 10000,並且小於等於 20000 的記錄
    我們可以用>= 和<=來實現,語句
select * from T_ACCOUNT
where usenum>=10000 and usenum<=20000

我們也可以用 between
.. and
..來實現

select * from T_ACCOUNT
where usenum between 10000 and 20000
  1. 空值查詢
    需求:查詢 T_PRICETABLE 表中 MAXNUM 為空的記錄
    語句:

select * from T_PRICETABLE t where maxnum is null
查詢結果:

技術分享圖片
需求:查詢 T_PRICETABLE 表中 MAXNUM 不為空的記錄
語句:

select * from T_PRICETABLE t where maxnum is not null
查詢結果:
技術分享圖片
(二)去掉重復記錄
需求:查詢業主表中的地址 ID,不重復顯示
語句:

select distinct addressid from T_OWNERS
[img=381,124]

(三)排序查詢
1.升序排序
需求:對 T_ACCOUNT 表按使用量進行升序排序
語句:

select * from T_ACCOUNT order by usenum
查詢結果:
技術分享圖片
2.降序排序
需求:對 T_ACCOUNT 表按使用量進行降序排序
語句:

select * from T_ACCOUNT order by usenum desc
查詢結果:
技術分享圖片
(四)基於偽列的查詢
在 Oracle 的表的使用過程中,實際表中還有一些附加的列,稱為偽列。偽列就
像表中的列一樣,但是在表中並不存儲。偽列只能查詢,不能進行增刪改操作。
接下來學習兩個偽列:ROWID 和 ROWNUM。
1ROWID
表中的每一行在數據文件中都有一個物理地址,ROWID 偽列返回的就是該行的
物理地址。使用 ROWID 可以快速的定位表中的某一行。ROWID 值可以唯一的
標識表中的一行。由於 ROWID 返回的是該行的物理地址,因此使用 ROWID 可
以顯示行是如何存儲的。
查詢語句:

select rowID,t.* from T_AREA t
查詢結果如下:
技術分享圖片
我們可以通過指定 ROWID 來查詢記錄

select rowID,t.*
from T_AREA t
where ROWID=‘AAAM1uAAGAAAAD8AAC‘;

查詢結果如下:
技術分享圖片
2ROWNUM
在查詢的結果集中,ROWNUM 為結果集中每一行標識一個行號,第一行返回 1,第二行返回 2,以此類推。通過 ROWNUM 偽列可以限制查詢結果集中返回的行數。[img=295,37]
查詢語句:

select rownum,t.* from T_OWNERTYPE t
查詢結果如下:
技術分享圖片
我們的分頁查詢需要用到此偽列,在本章第四小節詳細講解。
(五)聚合統計
ORACLE 的聚合統計是通過分組函數來實現的,與 MYSQL 一致。

  1. 聚合函數
    (1)求和 sum
    需求:統計 2012 年所有用戶的用水量總和

select sum(usenum) from t_account where year=‘2012‘

查詢結果如下:
 ![](http://i2.51cto.com/images/blog/201805/31/7881a55e66ea0c5ad2be6c5dbf8985f7.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
(2)求平均 avg
需求:統計 2012 年所有用水量(字數)的平均值

select avg(usenum) from T_ACCOUNT where year=‘2012‘
查詢結果如下:
 ![](http://i2.51cto.com/images/blog/201805/31/41d988d6b359f5db0d1cc7bb323e126e.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
(3)求最大值 max
需求:統計 2012 年最高用水量(字數)[img=168,44]

`select max(usenum) from T_ACCOUNT where year=‘2012‘`
查詢結果如下:
 ![](http://i2.51cto.com/images/blog/201805/31/b74638d7f8252f860219cbcc56615f8a.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
(4)求最小值 min
需求:統計 2012 年最低用水量(字數)

`select min(usenum) from T_ACCOUNT where year=‘2012‘`
查詢結果如下:
 ![](http://i2.51cto.com/images/blog/201805/31/1459333e5fe1d437322cf805fe31b36f.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
(5)統計記錄個數 count
需求:統計業主類型 ID 為 1 的業主數量

select count(*) from T_OWNERS t where ownertypeid=1

查詢結果如下:
 ![](http://i2.51cto.com/images/blog/201805/31/6fe428066f53e515664b5579df8290b9.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
2. 分組聚合 Group by
需求:按區域分組統計水費合計數
語句:

`select areaid,sum(money) from t_account group by areaid`
查詢結果:

![](http://i2.51cto.com/images/blog/201805/31/d816ce8c197cd1d3aed0516325aae8e0.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
3. 分組後條件查詢 having
需求:查詢水費合計大於 16900 的區域及水費合計
語句:

select areaid,sum(money) from t_account group by areaid
having sum(money)>169000


查詢結果:
 ![](http://i2.51cto.com/images/blog/201805/31/77729ecd463953b2dca238ed6ec33ba8.jpg?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

Oracle-day02 上