TSQL:即使条件不匹配,如何输出记录
TSQL: How to ouput records even when where condition doesnt match
我有一个 temp table 有 3 列 "ID","Cost", "MaxCost"..下面是我的 select 声明 selects 行给定特定 ID..
SELECT
t.Cost
t.MaxCost
FROM @temp t
WHERE t.ID = @ID
我如何修改上述查询,以便即使给定的 ID 不存在它仍然输出 Cost = 0 & MaxCost = 0 的行
Select实际和默认记录,select第一个按重量排序。
select top (1)
Cost,
MaxCost
from (
SELECT
t.Cost
t.MaxCost,
1 as takeme
FROM @temp t
WHERE t.ID = @ID
union all
select 0, 0, 0
) foo
order by
foo.takeme desc
declare @table table (cost int);
insert into @table values (2), (2), (3);
declare @findCost int = 1;
select * from @table where cost = @findCost
union all
select 0 as cost from @table where cost = @findCost having count(*) = 0;
set @findCost = 2;
select * from @table where cost = @findCost
union all
select 0 as cost from @table where cost = @findCost having count(*) = 0;
我有一个 temp table 有 3 列 "ID","Cost", "MaxCost"..下面是我的 select 声明 selects 行给定特定 ID..
SELECT
t.Cost
t.MaxCost
FROM @temp t
WHERE t.ID = @ID
我如何修改上述查询,以便即使给定的 ID 不存在它仍然输出 Cost = 0 & MaxCost = 0 的行
Select实际和默认记录,select第一个按重量排序。
select top (1)
Cost,
MaxCost
from (
SELECT
t.Cost
t.MaxCost,
1 as takeme
FROM @temp t
WHERE t.ID = @ID
union all
select 0, 0, 0
) foo
order by
foo.takeme desc
declare @table table (cost int);
insert into @table values (2), (2), (3);
declare @findCost int = 1;
select * from @table where cost = @findCost
union all
select 0 as cost from @table where cost = @findCost having count(*) = 0;
set @findCost = 2;
select * from @table where cost = @findCost
union all
select 0 as cost from @table where cost = @findCost having count(*) = 0;