[英]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.