Jump to content

Using GROUP BY but cannot get zero totals to show


joesoaper

Recommended Posts

I need to do a query that will show me all models and totals even if it is zero. The query only gives the models with 1 or more in stock. Sample here below.

 

Model 1                      5

Model 2                     15

Model 17                   10

 

This is the code I am using

 

                       $sql = 'SELECT status, model, COUNT(model) FROM club_inventory WHERE status="In-Stock" AND prod="Jets"  GROUP BY model ';
                       foreach ($pdo->query($sql) as $row) {
                                echo '<tr>';
                                echo '<td>'. $row['model'] . '</td>';
                                echo '<td>'. $row['COUNT(model)'] . '</td>';
                                echo '</td>';
                                echo '</tr>';
                       }

Any help would be greatly appreciated.

 

Link to comment
Share on other sites

How does a teacher check which pupils are absent from a class? S/he has a register of the pupils that should be there.

 

You are trying to count things that aren't there, so you need a "model" table containing a row for each model - your register.

 

You then query

SELECT i.status
 , m.model
 , COUNT(i.model)
FROM model m
LEFT JOIN club_inventory i
    ON m.model = i.model
    AND status="In-Stock"
    AND prod="Jets"  
GROUP BY m.model
Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.