为整个结果集添加一个值

时间:2011-12-07 14:28:09

标签: sql

我正在从表中选择行

select name,age from tabl1

我需要添加另一列,它应该具有整个结果集的相同值。 价值来自:

select value from config where key = 'company'

如何将其合并为一个查询?

结果集应该看起来像

Name1 | Age1 | SameComapanyName
Name2 | Age2 | SameComapanyName
Name3 | Age3 | SameComapanyName
Name4 | Age4 | SameComapanyName

3 个答案:

答案 0 :(得分:4)

select t.name, t.age, c.value
from tabl1 t
cross join config c
where c.key = 'company'

<强>加了: 当需要从特定表中返回多个列时,它可能更合适。

此列和子查询之间的行为存在差异。当条件c.key = 'company'不满足时,连接将根本不返回记录,如果在子查询上它返回每行的null值。

已添加(在@alex评论之后):

cros join返回NxM条记录,但是在这种情况下不是。此处where条件仅限M到特定行/ -s。有时NxM结果可能是必需的,那么子查询将无法满足它。

当子查询返回多个行或多个列时,查询(子查询版本)失败(这就是为什么其他答案将子查询中的行限制为topmax的一个,这并不总是足够的)。 cross join会毫无问题地做到这一点,它更有弹性。

在桌子上进行的测试:

create table species ( -- 9999 rows
    id int identity(1, 1), -- unique
    scientific varchar(50), -- not unique
    english varchar(50) -- unique
)

create table genus ( -- 2233 rows
    id int identity(1, 1), -- unique
    name varchar(50) -- unique
)

在我执行每个查询之前:

dbcc freeproccache
dbcc dropcleanbuffers

查询:

select s.*, g.name
from species s
cross join genus g
where g.name = 'Pinaroloxias'

执行计划:

execution plan cross join

统计:

SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 5 ms.

(9999 row(s) affected)
Table 'species'. Scan count 1, logical reads 79, physical reads 0, read-ahead reads 79, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'genus'. Scan count 1, logical reads 12, physical reads 11, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(4 row(s) affected)

SQL Server Execution Times:
   CPU time = 46 ms,  elapsed time = 224 ms.

查询:

select s.*, (select g.name from genus g where g.name = 'Pinaroloxias')
from species s

执行计划:

execution plan subquery

统计:

SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 5 ms.

(9999 row(s) affected)
Table 'Worktable'. Scan count 1, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'genus'. Scan count 1, logical reads 12, physical reads 11, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'species'. Scan count 1, logical reads 79, physical reads 0, read-ahead reads 79, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(8 row(s) affected)

SQL Server Execution Times:
   CPU time = 31 ms,  elapsed time = 224 ms.

查询:

select s.*, (select max(g.name) from genus g where g.name = 'Pinaroloxias')
from species s

执行计划:

execution plan subquery with max

统计:

SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 31 ms.

(9999 row(s) affected)
Table 'species'. Scan count 1, logical reads 79, physical reads 0, read-ahead reads 79, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'genus'. Scan count 1, logical reads 12, physical reads 11, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(6 row(s) affected)

SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 229 ms.

如果我多次完成并计算平均值,那么测试会更好。但我没有时间。

答案 1 :(得分:2)

您可以在SELECT子句中使用子查询,如下所示:

select name,age,(select max(value) from config where key = 'company') from tab1;

答案 2 :(得分:1)

这将有效:

select name,age,(select top 1 value from config where key = 'company') from tab1

如果config.key不能保证唯一,则前1是为了防止子查询返回多行。 (假设OP使用的是Sql Server)