如何在PDO查询返回的数组中插入0

时间:2014-04-08 06:41:11

标签: php arrays

我有这个PDO查询,它返回特定平台上失败的测试用例数

$status = 'failed';
$allTest = $conn->prepare('SELECT COUNT(testRunID),platform FROM tooldata WHERE status = :status GROUP BY platform ORDER BY platform' );
$allTest->execute(array(':status' => $status));

我如何修改此查询,以便我可以为没有测试用例失败的所有平台插入0。

我可以使用 -

检查返回的行何时为空
foreach($conn->prepare('SELECT COUNT(testRunID),platform FROM tooldata WHERE status = :status GROUP BY platform ORDER BY platform') as $row):
    if($row['totalCOunt'] != 0):
        echo "WE HAVE Failed testcase";
    else:
        echo "WE DO NOT HAVE failed testcase";
    endif;
endforeach;

但是如何在输出数组中为这些行插入0?

请帮忙。

3 个答案:

答案 0 :(得分:0)

只需删除WHERE条款即可 试试这个......

SELECT COUNT(testRunID),platform FROM tooldata GROUP BY platform ORDER BY platform'

此查询将显示具有相应计数的所有平台。

答案 1 :(得分:0)

据我所知,你可能需要这样的东西

SELECT count(1), sum(if(status = 'failed'),1,0) platform 
FROM tooldata GROUP BY platform ORDER BY platform'

如果它不适合你 - 那么就不要抱怨,因为我不应该免费为你写一个完整的工作代码,但是给你一个想法,如何解决你的问题。所以 - 尝试理解这个想法,而不是盲目地复制/粘贴。并提供一些反馈。

答案 2 :(得分:-1)

在查询中使用mysql UNION来选择其他结果:

SELECT 0, platform from tooldata where status != :status GROUP BY platform
    union select COUNT(testRunID),platform FROM tooldata WHERE status = :status GROUP BY platform ORDER BY platform

因此:

$status = 'failed';
$allTest = $conn->prepare('SELECT 0, platform from tooldata where status != :status GROUP BY platform
        union select COUNT(testRunID),platform FROM tooldata WHERE status = :status GROUP BY platform ' );
$allTest->execute(array(':status' => $status));