簡體   English   中英

從表中選擇列

[英]Selecting from table into a column

我在table1有一個名為Status的( bit類型)列,該列指示為0 = Stopped, 1 = Started並且有一個日志表,其IDtable1 ,該行的存在表明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.

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