Sql 使用IF语句为条目提取布尔列标题
我对IF语句一点也不在行。我目前的日程安排如下:Sql 使用IF语句为条目提取布尔列标题,sql,if-statement,boolean,Sql,If Statement,Boolean,我对IF语句一点也不在行。我目前的日程安排如下: Lot(Int) PartNum(Varchar50) Amount(Int) IsPainted(Bool) IsInspected(Bool) Finished(Bool) 1 xxx-0191 500 1 1 0 2 xxx-0191 700 1 0
Lot(Int) PartNum(Varchar50) Amount(Int) IsPainted(Bool) IsInspected(Bool) Finished(Bool)
1 xxx-0191 500 1 1 0
2 xxx-0191 700 1 0 0
select lot, partnum, amount,
(case when Finished = 1 then 'Finished'
when IsInspected = 1 then 'Inspected'
when IsPainted = 1 then 'Painted'
) as status
我试图完成的是,我认为它必须由一个IF语句来处理,但我当然愿意使用这里最有效的方法,就是有一个查询,它将给出以下结果
Lot PartNum Amount Status
1 xxx-0191 500 Inspected
2 xxx-0191 700 Painted
我需要它做的是在布尔列中拖出最后一个可用的“True”或“1”列,然后在查询的“Status”列中显示该信息。使用
case
。大概是这样的:
Lot(Int) PartNum(Varchar50) Amount(Int) IsPainted(Bool) IsInspected(Bool) Finished(Bool)
1 xxx-0191 500 1 1 0
2 xxx-0191 700 1 0 0
select lot, partnum, amount,
(case when Finished = 1 then 'Finished'
when IsInspected = 1 then 'Inspected'
when IsPainted = 1 then 'Painted'
) as status
这将选择最后一个布尔值作为为状态选择的布尔值。使用此代码
select
Lot,
PartNum,
Aamount,
Case when IsInspected=1 then 'Inspected' else 'Painted' end Status
from
table
用您正在使用的数据库标记您的问题。我必须定制添加“=1”才能使其工作,这是正确的还是我遗漏了此数据库中的某些内容?我非常感谢你的帮助@戴维霍尔特。这取决于您使用的数据库。您没有指定数据库,并明确表示列为布尔值。即使支持布尔类型的数据库也会在
when
子句中接受该类型。