您可以使用:
select
(Select Count(*) as StockCountA from Table_A where dept='AAA') as StockCountA,
(Select Count(*) as StockCountB from Table_B where dept='BBB') as StockCountB
说明:您可以选择单值作为select语句中的字段,所以你可以编写类似
select
x.*,
(select Value from Table_Y y) as ValueFromY
from
Table_X x
这仅当次查询返回至多1个行将工作。对于0行,ValueFromY将返回NULL
并且具有多于一行的查询将失败。
的select
的附加功能(在SQL Server,MySQL和可能其他人)是你可以选择只值,而不指定表可言,像这样:
Select
3.14 as MoreOrLessPI
可以这两个事实结合起来,该两项罪名合并成一个单一的结果,通过编写一个查询,看起来像:
Select
(Select query that returns at most 1 row) as Result1,
(Select another query that returns at most 1 row) as Result2
谢谢,如果你知道基础知识,那么你知道你在做什么,否则你是一个专家:) – Rosebud 2014-12-01 22:13:43
是的,这就是为什么我决定添加一点解释毕竟。很难找到正确的文档,我不知道要寻找什么。 :) – GolezTrol 2014-12-01 22:14:19