Oracle表的常用查詢實驗(五)

2021-08-27 15:39:02 字數 2133 閱讀 5693

1.問題描述:

test表中有id(人員編號),a(考核標準),b(實際得分),c(課程編號)四個字段,乙個id可能會有多個科目的評分,如果乙個id中存在a=b,則合格,求合格的人員編號。

2.需求分析:

要得到的結果為:

id 是否合格

1011 合格

1012 合格

1013 合格

1014 不合格

1015 合格

如果直接用decode()函式,則會出現同一id有合格和不合格的成績,錯誤

故合格產品滿足以下兩個條件:(1)id不能重複—》distinct (2)同一id存在a=b

3.解答過程:

(1)查出合格的

select

distinctid,

'合格'

pj from

test

where

idin

(select

idfrom

test

wherea=

b)(2)

查出不合格的

select

distinctid,

'不合格'

pj from

test

where

idnotin(

select

idfrom

test

wherea=

b)(3)使用union聯接

select

distinctid,

'合格'

pj from

test

where

idin

(select

idfrom

test

wherea=

b)union

select

distinctid,

'不合格'

'合格'

pj from

test

where

idin

(select

idfrom

test

wherea=

b)union

select

distinctid,

'不合格'

pj from

test

where

idnotin(

select

idfrom

test

wherea=

b);

5.聯想擴充套件:

假設只有a,b兩列資料,如果存在a=b,則顯示匹配成功(即根據a來判斷)

select

t3.xx

,decode

(t3.xx

,t3.yy

,'success'

,'fail'

)匹配情況

Oracle表的常用查詢實驗(六)

1.問題描述 為什麼第乙個sql有資料,第二個sql沒有資料?sql1 select t.from alx material types intf v t where t.material level 3 and t.material type not in select a.parent type...

Oracle常用表查詢

檢視所有的資料檔案 select from dba data files 檢視當前使用者 select from user users 檢視所有使用者 select from all users 檢視使用者或角色系統許可權 select from user sys privs 檢視角色所包含的許可權...

oracle的查詢資料表(五)

判斷null值 當要檢查列中是否包含空值的時候,需要使用is null 或者is not null語句,null通常為空值,空值的意思就是未指定的,不存在的值,不能與空白值相混淆,空白值是乙個村莊的,只是值為空白的值,使用邏輯組合在where子句中除了使用單個布林表示式外,還可以通過使用邏輯條件組合...