在php中有效地计算结果

时间:2015-01-27 12:36:35

标签: php mysql pdo count

使用的技术:php通过PDO访问MySQL。

我有以下查询:

    try {
        $stmt = $conn->prepare("SELECT * FROM $database.appsLodged WHERE `uID` = :uid");
        $stmt->bindValue(':uid', $uid);
        $stmt->execute();
        $result = $stmt->fetchAll();
    } catch(PDOException $e) { catchMySQLerror($e->getMessage()); }

这是相当直接的。目前相关数据库中有7行,并且每一行都可能为$ result [0] [5]返回不同的字符串值。我想能算出有多少结果有“鞋子和袜子”的结果[0] [5],然后有多少人有“靴子和裙子”的结果[0] [5]有多少人有“跳线和开襟羊毛衫”的$ result [0] [5] - 无需借助多个数据库查询(我知道我们可以通过添加额外的“AND item =:item来实现“语法和绑定:项目到”鞋子和袜子“,但这似乎非常重复和不必要。

建议,欢迎大家! :)

C

如下所述,这里是列出的$ result数组:

Array
(
[0] => Array
    (
        [id] => 1
        [0] => 1
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Pending
        [5] => Pending
        [dateStarted] => 1421773018
        [6] => 1421773018
        [lastModified] => 1421773018
        [7] => 1421773018
    )

[1] => Array
    (
        [id] => 2
        [0] => 2
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1421774389
        [6] => 1421774389
        [lastModified] => 1421774389
        [7] => 1421774389
    )

[2] => Array
    (
        [id] => 3
        [0] => 3
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1421776146
        [6] => 1421776146
        [lastModified] => 1421776146
        [7] => 1421776146
    )

[3] => Array
    (
        [id] => 4
        [0] => 4
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1421777460
        [6] => 1421777460
        [lastModified] => 1421777460
        [7] => 1421777460
    )

[4] => Array
    (
        [id] => 5
        [0] => 5
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1421781756
        [6] => 1421781756
        [lastModified] => 1421781756
        [7] => 1421781756
    )

[5] => Array
    (
        [id] => 6
        [0] => 6
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1422213946
        [6] => 1422213946
        [lastModified] => 1422213946
        [7] => 1422213946
    )

[6] => Array
    (
        [id] => 7
        [0] => 7
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1422238026
        [6] => 1422238026
        [lastModified] => 1422238026
        [7] => 1422238026
    )

[7] => Array
    (
        [id] => 8
        [0] => 8
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1422369458
        [6] => 1422369458
        [lastModified] => 1422369458
        [7] => 1422369458
    )

[8] => Array
    (
        [id] => 9
        [0] => 9
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1422369473
        [6] => 1422369473
        [lastModified] => 1422369473
        [7] => 1422369473
    )

[9] => Array
    (
        [id] => 10
        [0] => 10
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1422371233
        [6] => 1422371233
        [lastModified] => 1422371233
        [7] => 1422371233
    )

[10] => Array
    (
        [id] => 11
        [0] => 11
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1422371291
        [6] => 1422371291
        [lastModified] => 1422371291
        [7] => 1422371291
    )

[11] => Array
    (
        [id] => 12
        [0] => 12
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1422372793
        [6] => 1422372793
        [lastModified] => 1422372793
        [7] => 1422372793
    )

[12] => Array
    (
        [id] => 13
        [0] => 13
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1422373414
        [6] => 1422373414
        [lastModified] => 1422373414
        [7] => 1422373414
    )

[13] => Array
    (
        [id] => 14
        [0] => 14
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1422373681
        [6] => 1422373681
        [lastModified] => 1422373681
        [7] => 1422373681
    )

[14] => Array
    (
        [id] => 15
        [0] => 15
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1422373927
        [6] => 1422373927
        [lastModified] => 1422373927
        [7] => 1422373927
    )

[15] => Array
    (
        [id] => 16
        [0] => 16
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1422374004
        [6] => 1422374004
        [lastModified] => 1422374004
        [7] => 1422374004
    )

[16] => Array
    (
        [id] => 17
        [0] => 17
        [appID] => 4
        [1] => 4
        [applicationKey] => ConSupAp
        [2] => ConSupAp
        [applicationName] => Conference Support Application
        [3] => Conference Support Application
        [uID] => 1
        [4] => 1
        [status] => Data Acquisition
        [5] => Data Acquisition
        [dateStarted] => 1422374587
        [6] => 1422374587
        [lastModified] => 1422374587
        [7] => 1422374587
    )

)

重要的是要记住 - 它实际上是我想要计算的“状态”列,除了第一个之外,所有这些示例都被设置为“数据采集”。第一个设置为“待定”。这可能还有另外三个值,如果这些值不在数据库中,那么我需要对它们进行零计数,这样用户就知道他们没有将数据设置为其他潜在的“状态”设置。

1 个答案:

答案 0 :(得分:0)

我建议您执行以下操作:首先,您从数据库中获取所有这些字符串,因此您将拥有$result[0][5]$result[1][5]$result[2][5]等内容,然后你foreach()他们并匹配不同变量中的字符串,每个字符串对应一个你要匹配的字符串...我认为,根据你在查询中所说的,每个uID都有一种类型的字符串在[ 5]位置。在女巫的情况下,我建议你有一串id,所以你的查询会变成这样的:

$uid = '1,2,3,4,5'; // for example
$stmt = $conn->prepare("SELECT * FROM $database.appsLodged WHERE `uID` IN (:uid)");

$stmt->bindValue(':uid', $uid);

您可能需要一个额外的查询才能获取所有这些ID但是有关它的信息:D

希望这有帮助!