Php pdo-将多个表的行数作为单独的查询获取?

Php pdo-将多个表的行数作为单独的查询获取?,php,mysql,sql,pdo,Php,Mysql,Sql,Pdo,我需要使用pdo的相同条件从多个表中获取行数。例如,这将为表用户提供行计数 $stmt = $db->prepare("SELECT * FROM `users` WHERE `account_id` = ? AND (`computer_name` = 'name1' OR `computer_name` = 'name2' OR `computer_name` = 'name3'); $stmt->execute(array($_SESSION['user']['account_

我需要使用pdo的相同条件从多个表中获取行数。例如,这将为表
用户提供行计数

$stmt = $db->prepare("SELECT * FROM `users` WHERE `account_id` = ? AND (`computer_name` = 'name1' OR `computer_name` = 'name2' OR `computer_name` = 'name3');
$stmt->execute(array($_SESSION['user']['account_id']));

$result = $stmt->fetchColumn();
现在,我想运行相同的查询,但使用不同的表。。。例如<代码>用户1
用户2
用户3
用户4
,等等

当然,这些表都是不同的,但都有列
account\u id
computer\u name
。是否有可能以某种方式连接所有这些表,并且仍然获得每个表的计数,或者我需要为每个表对数据库运行多个选择

目标是为登录用户创建一个显示,显示数据库中不同表的记录数

预期结果是,我可以获得满足where子句条件的每个表的总行数:

users1 = 50
users2 = 34
users3 = 82
...etc

是的,为了获得计数,您必须将所有数据表连接到相应的外键上。

您可以执行以下查询:

SELECT 'users' as tbl, count(*) as cnt FROM `users` 
WHERE `account_id` = ? AND (`computer_name` = 'name1' OR `computer_name` = 'name2' OR `computer_name` = 'name3')
UNION
SELECT 'users2' as tbl, count(*) as cnt FROM `users2` 
WHERE `account_id` = ? AND (`computer_name` = 'name1' OR `computer_name` = 'name2' OR `computer_name` = 'name3')
UNION
SELECT 'users3' as tbl, count(*) as cnt FROM `users3` 
WHERE `account_id` = ? AND (`computer_name` = 'name1' OR `computer_name` = 'name2' OR `computer_name` = 'name3')
然后传递适当数量的参数:

$stmt->execute(array_fill(0, 3, $_SESSION['user']['account_id']));

但我还能得到每个特定表的计数吗?Users1=50,users2=350,等等……这真是太棒了。。。那么,有没有可能在一个特定的表上分解另一个级别?比如说,在表users2上,我在where子句中添加了
type
,比如'something'。。。由于我使用的是同一个表,我还会进行一个全新的联合吗?是的,在这种情况下,您需要添加另一个
联合
块,即使是在同一个表上。使用不同的
WHERE
子句的每个计数都需要一个新的
联合。谢谢。。。我假设这将比每个单独的查询占用更少的资源,因为只有一个调用,对吗?