簡體   English   中英

如果另一列具有值,則選擇“虛擬”列,或者在SQL視圖中為null

[英]Select a 'virtual' column if another column has a value or is null in a SQL view

我想在視圖中選擇ALWAYS列,並使用CASES進行設置。 例:

SELECT isDone
,doneN

from...

我必須以這種方式設置isDone:

if done is 1 isDone is 'yes'
if done is 0 isDone is 'no'

(doneN是來自另一個表的列,isDone在其他表中不存在,因此它是“虛擬”列)

謝謝你的建議

這是你想要的嗎?

select (case when doneN = 1 then 'yes' else 'no' end) as isDoneN
. . . 

您沒有指定'done'是否始終為0或1.如果有更多值或您想要捕獲其他值,請使用以下內容:

select (case when done = 1 then 'yes' 
             when done = 0 then 'no' 
             else '' end) as isDone 
, doneN
from ...

如果'done'被約束為0或1,您可以使用:

select (case when done = 1 then 'yes' 
             else 'np' end) as isDone 
, doneN
from ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM