比如我現在有兩張表
人員資訊表
name | home | homeNow
張三 | 01 | 02
李四 | 02 | 02
地區表
id | name
01 | 北京
02 | 上海
03 | 廣東
home=家鄉地址
homeNow = 現住址
那么我現在想查詢每個人的資訊,就是這樣
select 人員資訊表.name 姓名,地區表.name 家鄉地址,地區表.name 現住址 from 人員資訊表,地區表 where 不知道咋寫
這種判斷怎么寫啊?一個欄位,可以用在兩個地方。暈了,求大神賜教!
uj5u.com熱心網友回復:
select a.name 姓名,b.name 家鄉地址
,c.name 現住址
from 人員資訊表 a
left join 地區表 b on a.home=b.id
left join 地區表 c on a.homenow = c.id
uj5u.com熱心網友回復:
select name,decode(home,'01','北京',,'02','上海','03','廣東')
,decode(homeNow,'01','北京',,'02','上海','03','廣東')
from 人員資訊表
判斷的寫法:decode也可以換成 case when
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/238740.html
標籤:開發
上一篇:這個SQL 怎么寫
