简体   繁体   中英

How to insert 0 in the array returned by PDO query

I have this PDO query which return me the count of test-cases failed on a particular platform

$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));

how i can modify this query, So that I can insert 0 for all those platform on which no test-case is failed.

I can check when the returned row is null with -

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;

but how to insert 0 for such rows in the output array ?

Please help.

Just remove the WHERE clause.
Try this...

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

This query will show all the plateforms with respective count.

As far as I can tell from your description, you may be need something like this

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

if it doesn't work for you - then don't complain, as I am not supposed to write a complete working code for you for free, but give you an idea, how to solve your problem. So - try to understand the idea, instead of just mindlessly copy/paste. And give some feedback.

Use mysql UNION in your query to select other results :

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

as such :

$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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM