[英]Selecting from table into a column
我在table1
有一個名為Status
的( bit
類型)列,該列指示為0 = Stopped, 1 = Started
並且有一個日志表,其ID
為table1
,該行的存在表明table1
中的Status
已損壞。
如何創建覆蓋這三種狀態的select
語句? 我正在考慮創建一個臨時表,該表具有所有選定的列以及一個新列( NewStatus
),該新列從Status
列中獲取它的值以及一個subselect語句,該語句為table1
的行ID過濾table2
。 但是我無法想象我該怎么做!
有沒有更好的辦法?
使用left join
select table1.*,
case when table2.id is null then
case table1.status
when 0 then 'stopped'
when 1 then 'started'
end
else 'damaged'
end
from
table1 left join table2 on table1.id = table2.id
您可能會使用isnull
掩蓋您的意圖,這可能會更快也可能不會更快。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.