从表格中选择一列

时间:2013-08-05 09:37:15

标签: sql sql-server tsql

我在bit中有一个名为Status的{​​table1类型)列,表示0 = Stopped, 1 = Started,并且有一个ID {{}}的日志表{1}},行存在表示table1中的Status已损坏。

如何创建涵盖三种状态的table1语句? 我正在考虑创建一个临时表,其中包含所有选定列以及一个新列(select),该列从NewStatus列获取值,并为子列表{i}过滤Status table2中的行ID。但我无法想象我能做到这一点!

有没有更好的方法?

1 个答案:

答案 0 :(得分:1)

使用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来模糊您的意图,这可能会更快或更快。

相关问题