简体   繁体   中英

Combining multiple sub-selects into one result row

I'm a bit stuck on this supposedly basic SQL and would appreciate some pointers.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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