我是 SQL/MySQL 的新手,我正在練習 SQL 和資料庫。我使用MS Server Studio并輸入:“Select DocumentLevel,Title,Owner From Production.Document”,顯示如下圖:
我像這樣使用 SQL:
Select REPLACE(Owner,'217','Abby') From Production.Document
但它只顯示了一個沒有標題的列,只有一個數字 217 被替換了。
你能幫我用 SQL 來得到像2這樣的表嗎?
uj5u.com熱心網友回復:
該數字很可能是來自另一個表的標識。
所以用它從另一個表中獲取資料。
讓我們假設另一個表是 Production.Persons
并且它有列: ID, Name
其中 ID 是 Production.Persons 的主鍵
然后加入會起作用
Select DocumentLevel, Doc.Title, Owner.Name
From Production.Document As Doc
Left Join Production.Persons As Owner On Owner.ID = Doc.Owner
但是,如果你被困在沒有桌子的情況下,而老師不允許。
然后你可以使用 a CASEorCASE WHEN邏輯。
Select DocumentLevel, Title
, Case Owner
When 217 Then 'Abby'
When 219 Then 'Billy'
When 220 Then 'Cindy'
End As Owner
From Production.Document
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/382458.html
標籤:sql sql-server
