sql server如何在一行中组合多个结果?

时间:2013-10-29 03:38:25

标签: sql subquery

如何在1行中显示这3个结果?我正在使用Nortwnd示例db:

select top 1 CompanyName from dbo.Customers
select top 1 LastName from employees
select top 1 categoryname from dbo.Categories

我尝试了Union并且相交但是无法得到结果。

所以我想要像:

CompanyName         | LastName | CategoryName
Alfreds Futterkiste | Buchanan | Beverages

2 个答案:

答案 0 :(得分:2)

我不明白为什么你会想要这样的结果,但以下会得到你想要的东西:

SELECT TOP 1 CompanyName,
(
  SELECT TOP 1 LastName FROM dbo.employees
) AS TopLastName,
(
  SELECT TOP 1 categoryname FROM dbo.Categories
) AS TopCategory
FROM dbo.Customers

答案 1 :(得分:0)

试试这个:

select 
CompanyName  = (select top 1 CompanyName from dbo.Customers),
LastName = (select top 1 LastName from employees),
CategoryName = (select top 1 categoryname from dbo.Categories)