0

我有两个条件的 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]
4

1 回答 1

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 语法,而不是 92 之前的东西,并且为了 Cthulhu 的缘故,至少为您的列和表选择一种命名约定。

哦,你可能必须在 No_ 前面加上它所在的任何表,因为我不知道。

于 2012-12-11T10:54:50.827 回答