joesoaper Posted July 10, 2016 Share Posted July 10, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301460-using-group-by-but-cannot-get-zero-totals-to-show/ Share on other sites More sharing options...
Barand Posted July 10, 2016 Share Posted July 10, 2016 Your code is like walking into a room and asking "Will anyone who isn't here please raise their hand". Quote Link to comment https://forums.phpfreaks.com/topic/301460-using-group-by-but-cannot-get-zero-totals-to-show/#findComment-1534392 Share on other sites More sharing options...
joesoaper Posted July 10, 2016 Author Share Posted July 10, 2016 I have no idea what that means, if you cannot give any help just say so, if my code it totally wrong say so as well, then at least I will have an idea of where I am wrong and I can try and fix it.. Quote Link to comment https://forums.phpfreaks.com/topic/301460-using-group-by-but-cannot-get-zero-totals-to-show/#findComment-1534393 Share on other sites More sharing options...
Barand Posted July 10, 2016 Share Posted July 10, 2016 (edited) 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 July 10, 2016 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/301460-using-group-by-but-cannot-get-zero-totals-to-show/#findComment-1534395 Share on other sites More sharing options...
joesoaper Posted July 10, 2016 Author Share Posted July 10, 2016 Thanks for that, I really appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/301460-using-group-by-but-cannot-get-zero-totals-to-show/#findComment-1534396 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.