简体   繁体   English

将多个子选择组合到一个结果行中

[英]Combining multiple sub-selects into one result row

I'm a bit stuck on this supposedly basic SQL and would appreciate some pointers.我有点坚持这个所谓的基本 SQL 并希望得到一些指示。

I'd like to get a single row result from combining multiple sub-selects.我想通过组合多个子选择获得单行结果。 What I have so far (which of course does not work):到目前为止我所拥有的(当然不起作用):

select * from (

    (select count(*) from a where name='a') as a),
    (select count(*) from b where name='d') as b)

) as foo;

...and I'm looking for a result along the lines of: ...我正在寻找以下结果:

a | b
-----
1 | 2

Given the source tables:给定源表:

Table a:
 id | name
----+------
  1 | a
  2 | b
  3 | c

Table b:
 id | name
----+------
  1 | a
  2 | b
  3 | c
  4 | d
  5 | d

I also tried something along the lines of我也尝试了一些类似的东西

select count(a.*), count(b.*) from a, b where a.name='a' and b.name='d';

which produces:产生:

count | count
------+-------
    2 |     2

I'd appreciate any assistance.我会很感激任何帮助。 thanks谢谢

Just use:只需使用:

select (select count(*) from a where name='a') as a,
       (select count(*) from b where name='d') as b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM