使用MYSQL在一个查询中的两个表中选择COUNT

Bru*_*uce 0 mysql sql

有没有办法可以选择带有两个表和一个查询的COUNT.

目前,我有以下,它无法正常工作.

SELECT 
COUNT(t1.id) as t1_amount,
COUNT(t2.id) as t2_amount
FROM
table1 t1,
table2 t2
Run Code Online (Sandbox Code Playgroud)

Gor*_*off 6

这是一种方式:

select (select count(*) from table1) as t1_amount,
       (select count(*) from table2) as t2_amount
Run Code Online (Sandbox Code Playgroud)

这是另一种方式:

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
Run Code Online (Sandbox Code Playgroud)

你的方法不起作用,因为,from子句中做了一个cross join.这是两个表之间的笛卡尔积.