检查两个sql条件并将其显示在同一列中

时间:2012-12-11 10:12:12

标签: sql-server-2008 asp-classic

我有两个条件的SQL结果..我需要在asp中的同一列中打印这两个条件。

sql查询

select P.[Port Name], CR.Name as country,CASE
when SIH.[Ship-to Code] = '' then (select C.Name  from [Customer] C where C.No_ = SIH.[Sell-to Customer No_])
end as name,
case 
when SIH.[Ship-to Code] = '' then (select C.Address  from [Customer] C where C.No_ = SIH.[Sell-to Customer No_])
end as addr, 
case
when SIH.[Ship-to Code] = '' then  (select C.[City] from [Customer] C where C.No_ = SIH.[Sell-to Customer No_])
end as city
from [Sales Invoice Header] SIH, [Port] P, [Country_Region] CR where No_ = 'PEXP1213-523' and P.Code = SIH.Port
and CR.Code = SIH.[Country of Final Destination]
union all
select P.[Port Name], CR.Name as country,
case
when SIH.[Ship-to Code] <> '' then (select C.Name from [Ship-to Address] C where C.Code = SIH.[Ship-to Code] and C.[Customer No_] = SIH.[Sell-to Customer No_])
end as name,
case
when SIH.[Ship-to Code] <> '' then  (select C.[Address] from [Ship-to Address] C where C.Code = SIH.[Ship-to Code] and C.[Customer No_] = SIH.[Sell-to Customer No_])
end as addr,
case
when SIH.[Ship-to Code] <> '' then  (select C.[City] from [Ship-to Address] C where C.Code = SIH.[Ship-to Code] and C.[Customer No_] = SIH.[Sell-to Customer No_])
end as city
 from [Sales Invoice Header] SIH, [Port] P, [Country_Region] CR where No_ = 'PEXP1213-524' and P.Code = SIH.Port
 and CR.Code = SIH.[Country of Final Destination]

1 个答案:

答案 0 :(得分:2)

select P.[Port Name], CR.Name as country, c.Name, c.Address, c.City
from [Sales Invoice Header] SIH
inner join Port P On P.Code = SIH.Port
inner join Country_Region CR On CR.Code = SIH.[Country of Final Destination]
inner join Customer C.No_ = SIH.[Sell-to Customer No_]) 
where No_ = 'PEXP1213-523' and SIH.[Ship_to Code] = ''
union all
select P.[Port Name], CR.Name as country, SIH.Name, SIH.Address,SIH.City
From [Sales Invoice Header] SIH
inner join Port P On P.Code = SIH.Port
inner join Country_Region CR On CR.Code = SIH.[Country of Final Destination]
Where No_ = 'PEXP1213-524' and SIH.[Ship_to Code] <> ''

更容易阅读,可能会帮助您找出问题所在。 使用ansi join语法,而不是pre 92的东西 而且对于Cthulhu来说,至少为你的列和表选择一个命名约定。

哦,你可能不得不在No_的前面加上任何表格,因为我没有线索。