簡體   English   中英

將多個子選擇組合到一個結果行中

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

我有點堅持這個所謂的基本 SQL 並希望得到一些指示。

我想通過組合多個子選擇獲得單行結果。 到目前為止我所擁有的(當然不起作用):

select * from (

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

) as foo;

...我正在尋找以下結果:

a | b
-----
1 | 2

給定源表:

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

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

我也嘗試了一些類似的東西

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

產生:

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

我會很感激任何幫助。 謝謝

只需使用:

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