[英]combine multiple count/group queries into one statement
我一直在研究一种为多个表中的用户返回计数的方法。
<?php
$stmt = $db->prepare("SELECT `computer_username`, `computer_name`, COUNT(`computer_name`) `totalsum` FROM `table1`
WHERE `account_id` = ? AND (`computer_name` = 'comp1' OR `computer_name` = 'comp2' OR `computer_name` = 'comp3')
GROUP BY `computer_username`, `computer_name`
");
$stmt->execute(array($_SESSION['user']['account_id']));
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
//print out the array
echo 'table1<br /><pre>',print_r($results,true),'</pre><br /><br />';
$stmt = $db->prepare("SELECT `computer_username`, `computer_name`, COUNT(`computer_name`) `totalsum` FROM `table2`
WHERE `account_id` = ? AND (`computer_name` = 'comp1' OR `computer_name` = 'comp2' OR `computer_name` = 'comp3')
GROUP BY `computer_username`, `computer_name`
");
$stmt->execute(array($_SESSION['user']['account_id']));
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
//print out the array
echo 'table2<br /><pre>',print_r($results,true),'</pre><br /><br />';
$stmt = $db->prepare("SELECT `computer_username`, `computer_name`, COUNT(`computer_name`) `totalsum` FROM `table3`
WHERE `account_id` = ? AND (`computer_name` = 'comp1' OR `computer_name` = 'comp2' OR `computer_name` = 'comp3')
GROUP BY `computer_username`, `computer_name`
");
$stmt->execute(array($_SESSION['user']['account_id']));
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
//print out the array
echo 'table3<br /><pre>',print_r($results,true),'</pre><br /><br />';
?>
对于每个数组,结果类似于以下内容:
Array
(
[0] => Array
(
[computer_username] => Bob
[computer_name] => comp1
[totalsum] => 1
)
[1] => Array
(
[computer_username] => Steve
[computer_name] => comp1
[totalsum] => 27
)
[2] => Array
(
[computer_username] => Sue
[computer_name] => comp2
[totalsum] => 7
)
)
我只是想返回数据库中每个表的每个组的总行数。 由于条件完全相同且仅表名不同,是否有一种方法可以在一次调用中返回全部内容? 我整天都在玩它,但是到目前为止,对每个查询使用查询是我获得结果的唯一方法。
理想情况下,我正在寻找一个类似以下内容的数组结果:
Array
(
[0] => Array
(
[computer_username] => Bob
[computer_name] => comp1
[table1] => 15
[table2] => 34
[table3] => 131
)
... and so on for each computer_name/computer_username group
)
编辑:
我在这方面取得了一些进展...
SELECT * FROM
(SELECT computer_name, username, COUNT(computer_name) usercount
FROM users
WHERE `account_id` = ?
GROUP BY `username`, `computer_name`) a
JOIN
(SELECT computer_name, computer_username, COUNT(computer_name) t1count
FROM t1
WHERE `account_id` = ?
GROUP BY `computer_username`, `computer_name`) b
JOIN
(SELECT computer_name, computer_username, COUNT(computer_name) t2count
FROM t2
WHERE `account_id` = ?
GROUP BY `computer_username`, `computer_name`) c
JOIN
(SELECT computer_name, computer_username, COUNT(computer_name) t3count
FROM t3
WHERE `account_id` = ?
GROUP BY `computer_username`, `computer_name`) d
ON a.username = b.computer_username AND a.username = c.computer_username AND a.username = d.computer_username
我从users
表开始,因为它将始终列出所有用户名/计算机组的组合。 问题在于,要在数组中返回的任何结果,用户名/计算机组也必须在每个其他表中都包含条目(t1,t2,t3)。
可以这么说,在computer1上的Bob在t1中有5行,在t2中有10行,在t3中有50行,我将把这些计数以t1count,t2count,t3count的形式返回到我的数组中。 在computer4上的Sue在t1、0和t2中具有5,在t3中具有12 ...因为t2中没有行,所以不会返回她的任何结果。
有什么办法解决这个问题,或者联接是否明确要求从一个表到另一个表的匹配值?
示例数据和预期结果:
//always contains every unique username/computer_name combination
`users` (username, computer_name)
bob, computer1
bob, computer8
steve, computer1
joe, computer3
sal, computer4
cindy, computer4
bill, computer8
jack, computer2
//contains data by computer_username/computer_name combo (can repeat)
`table1` (computer_username, computer_name)
bob, computer1
bob, computer8
bob, computer8
bob, computer8
steve, computer1
joe, computer3
joe, computer3
joe, computer3
bill, computer8
sal, computer4
sal, computer4
sal, computer4
cindy, computer4
bill, computer8
//contains data by computer_username/computer_name combo (can repeat)
`table2` (computer_username, computer_name)
bob, computer1
bob, computer1
bob, computer1
bob, computer8
bob, computer8
joe, computer3
joe, computer3
bill, computer8
sal, computer4
sal, computer4
cindy, computer4
cindy, computer4
cindy, computer4
//contains data by computer_username/computer_name combo (can repeat)
`table3` (computer_username, computer_name)
bob, computer8
steve, computer1
steve, computer1
steve, computer1
bill, computer8
bill, computer8
steve, computer1
sal, computer4
cindy, computer4
cindy, computer4
// resulting array on account_id = 1 and computer_name = (computer1, computer2, computer3, or computer4)
Array
(
[0] => Array
(
[computer_username] => bob
[computer_name] => computer1
[t1count] => 1
[t2count] => 3
[t3count] => 0
)
[1] => Array
(
[computer_username] => steve
[computer_name] => computer1
[t1count] => 1
[t2count] => 0
[t3count] => 3
)
[3] => Array
(
[computer_username] => joe
[computer_name] => computer3
[t1count] => 3
[t2count] => 2
[t3count] => 0
)
[4] => Array
(
[computer_username] => sal
[computer_name] => computer4
[t1count] => 3
[t2count] => 2
[t3count] => 1
)
[5] => Array
(
[computer_username] => cindy
[computer_name] => computer4
[t1count] => 1
[t2count] => 3
[t3count] => 2
)
[6] => Array
(
[computer_username] => jack
[computer_name] => computer2
[t1count] => 0
[t2count] => 0
[t3count] => 0
)
)
假设上面的所有表都有一个名为account_id = 1的列。您有一个account_id,它代表计算机的“所有者”。 用户是所有计算机以及这些计算机上所有用户名的表。 table1,table2,table3是与特定计算机和该计算机的特定用户相关联的记录。 在那些表中的任何特定用户/计算机对都可以有零个记录或任何数量的记录。
目标是在给定一个account_id和computer_name列表的情况下,为每个用户返回记录表(table1,table2,table3)的计数。 无需对结果进行任何排序。
使用提供的数据并且不提供要查询的ID即可完成您想要的操作。 看到工作FIDDLE
SELECT
usr,
cmptr,
t1_count,
t2_count,
t3_count
FROM (
SELECT
users.username AS usr,
users.computer_name AS cmptr,
count(t1.computer_name) AS t1_count
FROM users
JOIN t1 ON t1.computer_username = users.username AND t1.computer_name = users.computer_name
GROUP BY users.username, users.computer_name
)AS t
LEFT JOIN(
SELECT
users.username,
users.computer_name,
count(t2.computer_name) AS t2_count
FROM users
JOIN t2 ON t2.computer_username = users.username AND t2.computer_name = users.computer_name
GROUP BY users.username, users.computer_name
) AS te ON te.username = t.usr OR te.computer_name = t.cmptr
LEFT JOIN(
SELECT
users.username,
users.computer_name,
count(t3.computer_name) AS t3_count
FROM users
JOIN t3 ON t3.computer_username = users.username AND t3.computer_name = users.computer_name
GROUP BY users.username, users.computer_name
) AS tem ON tem.username = t.usr OR tem.computer_name = t.cmptr
GROUP BY usr, cmptr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.