比如,我現在有兩張表。學生表 和 班級表
學生表結構如下
name | classID | classIDBF
張三 | 01 | null
李四 | 02 | 02
王五 | null | 03
班級表結構如下
name | id
一班 | 01
二班 | 02
三班 | 03
現在,我要查詢 每個學生對應的是哪個班級。那么就是
select 學生表.name,班級表.name from 學生表,班級表 where 學生表.classID=班級表.id
可是,這樣的話,王五的班級就查不出來。
所以,我想實作這種,先判讀
學生表.classID=班級表.id
,如果沒有值,那么就再查詢
學生表.classIDBF=班級表.id
然后讀出值。
有點類似于C# 的 if 判斷一樣。請問有大佬會嗎?
uj5u.com熱心網友回復:
select a.name,b.name
from 學生表 a
left join 班級表 b on nvl(a.classID ,a.classIDBF) = b.id
uj5u.com熱心網友回復:
我也沒試過,上面的要是不行,就試試這個。select a.name
,b.name
from (
select name
,nvl(classID ,classIDBF) classID
,classIDBF
from 學生表
) a
left join 班級表 b on a.classID = b.id
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/238741.html
標籤:開發
