0

1 つのクエリで 2 つのテーブルを持つ COUNT を選択する方法はありますか。

現時点では、次のものがあり、正しく動作しません。

SELECT 
COUNT(t1.id) as t1_amount,
COUNT(t2.id) as t2_amount
FROM
table1 t1,
table2 t2
4

2 に答える 2

7

1 つの方法を次に示します。

select (select count(*) from table1) as t1_amount,
       (select count(*) from table2) as t2_amount

別の方法は次のとおりです。

select t1.t1_amount, t2.t2_amount
from (select count(*) as t1_amount from table1) t1 cross join
     (select count(*) as t2_amount from table2) t2

句の が,を行うため、メソッドは機能しません。これにより、2 つのテーブル間でデカルト積が計算されます。fromcross join

于 2013-04-18T01:12:01.823 に答える