Pdo一次计算多个表

问题描述 投票:0回答:1

有没有办法用一个查询计算多个表:

table 1 => 174 rows // $x
table 2 => 3824 rows // $y
table 3 => 295 rows //$z

然后使用然后回显那些计数:

echo 'Table one has ' . $x . ' rows';
echo 'Table two has ' . $y . ' rows';
echo 'Table three has ' . $z . ' rows';

如果有办法可以帮助我了解它的工作原理吗?

谢谢!:D

mysql pdo count
1个回答
1
投票

您可以使用UNION:

SELECT 'table1' AS name, COUNT(*) AS rows FROM table1
UNION
SELECT 'table2', COUNT(*) FROM table2
UNION
SELECT 'table3', COUNT(*) FROM table3

产量

name     rows
table1   174
table2   3824
table3   295
© www.soinside.com 2019 - 2024. All rights reserved.