将多个计数/组查询组合到一个语句中

时间:2014-05-06 00:59:51

标签: php mysql sql pdo

我一直在玩一种方法来返回多个表中用户的计数。

<?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)。

所以说,计算机1上的Bob在t1中有5行,在t2中有10行,在t3中有50行我会将这些计数作为t1count,t2count,t3count返回到我的数组中。计算机4上的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代表&#39;所有者&#39;一台电脑用户是这些计算机上所有计算机和所有用户名的表。 table1,table2,table3是绑定到特定计算机和该计算机的特定用户的记录。任何这些表中的任何特定用户/计算机对都可以有零个或任意数量的记录。

目标是在给定account_id和computer_name列表的情况下为每个用户返回记录表(table1,table2,table3)的计数。结果不需要任何类型的订购。

1 个答案:

答案 0 :(得分:1)

提供的数据并没有提供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