The Little Guy Posted May 2, 2010 Share Posted May 2, 2010 I have a table that has a column that has the week day (Sunday - Saturday). I have a query, and I ALWAYS want it to return 7 rows (Sunday - Saturday). SELECT COUNT(*) as total, weekDay FROM sts_cal WHERE pid = '1' GROUP BY weekDay if the column total doesn't have anything I want it to say 0, otherwise put the total... Does this make sense? Quote Link to comment https://forums.phpfreaks.com/topic/200490-return-7-rows-always/ Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 I think so. What are the results you get back? Quote Link to comment https://forums.phpfreaks.com/topic/200490-return-7-rows-always/#findComment-1052085 Share on other sites More sharing options...
Zane Posted May 2, 2010 Share Posted May 2, 2010 LIMIT 7 Quote Link to comment https://forums.phpfreaks.com/topic/200490-return-7-rows-always/#findComment-1052086 Share on other sites More sharing options...
The Little Guy Posted May 2, 2010 Author Share Posted May 2, 2010 LIMIT 7 wont work. Currently my table has 4 values in it, all are "Sunday" (today). I want to return "Monday", "Tuesday", etc. but those days are not in there. Right now it returns this: total weekDay 4 Sunday I would like it to return this: total weekDay 4 Sunday 0 Monday 0 Tuesday 0 Wednesday 0 Thursday 0 Friday 0 Saturday does that make better sense? total will change weekDay will not, but there will ALWAYS be those seven thing returned. Possible? Quote Link to comment https://forums.phpfreaks.com/topic/200490-return-7-rows-always/#findComment-1052093 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 You're trying to select something that's not there? Quote Link to comment https://forums.phpfreaks.com/topic/200490-return-7-rows-always/#findComment-1052107 Share on other sites More sharing options...
The Little Guy Posted May 2, 2010 Author Share Posted May 2, 2010 Yes Quote Link to comment https://forums.phpfreaks.com/topic/200490-return-7-rows-always/#findComment-1052111 Share on other sites More sharing options...
The Little Guy Posted May 4, 2010 Author Share Posted May 4, 2010 So... Is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/200490-return-7-rows-always/#findComment-1052842 Share on other sites More sharing options...
trq Posted May 4, 2010 Share Posted May 4, 2010 if the column total doesn't have anything I want it to say 0, otherwise put the total... So write that logic into your loop while your outputting the results. Nothing to do with mysql. Quote Link to comment https://forums.phpfreaks.com/topic/200490-return-7-rows-always/#findComment-1052853 Share on other sites More sharing options...
fenway Posted May 4, 2010 Share Posted May 4, 2010 You're trying to select something that's not there? You just need to join in a derived table that contains one row for each required date. Either use an integers table to generate it, or just UNION 7 records. Quote Link to comment https://forums.phpfreaks.com/topic/200490-return-7-rows-always/#findComment-1052952 Share on other sites More sharing options...
The Little Guy Posted May 4, 2010 Author Share Posted May 4, 2010 Thanks Fenway! SELECT COUNT(*) as total, 'Sunday' as lbl FROM sts_cal WHERE pid = '{$this->pid}' AND weekDay = 'Sunday' UNION SELECT COUNT(*) as total, 'Monday' as lbl FROM sts_cal WHERE pid = '{$this->pid}' AND weekDay = 'Monday' UNION SELECT COUNT(*) as total, 'Tuesday' as lbl FROM sts_cal WHERE pid = '{$this->pid}' AND weekDay = 'Tuesday' UNION SELECT COUNT(*) as total, 'Wednesday' as lbl FROM sts_cal WHERE pid = '{$this->pid}' AND weekDay = 'Wednesday' UNION SELECT COUNT(*) as total, 'Thursday' as lbl FROM sts_cal WHERE pid = '{$this->pid}' AND weekDay = 'Thursday' UNION SELECT COUNT(*) as total, 'Friday' as lbl FROM sts_cal WHERE pid = '{$this->pid}' AND weekDay = 'Friday' UNION SELECT COUNT(*) as total, 'Saturday' as lbl FROM sts_cal WHERE pid = '{$this->pid}' AND weekDay = 'Saturday' Quote Link to comment https://forums.phpfreaks.com/topic/200490-return-7-rows-always/#findComment-1053107 Share on other sites More sharing options...
fenway Posted May 4, 2010 Share Posted May 4, 2010 Not what I meant, but that works too. Quote Link to comment https://forums.phpfreaks.com/topic/200490-return-7-rows-always/#findComment-1053144 Share on other sites More sharing options...
The Little Guy Posted May 4, 2010 Author Share Posted May 4, 2010 What did you mean? Is there a better more efficient way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/200490-return-7-rows-always/#findComment-1053251 Share on other sites More sharing options...
fenway Posted May 4, 2010 Share Posted May 4, 2010 Probably not in this case, assuming proper indexes. Quote Link to comment https://forums.phpfreaks.com/topic/200490-return-7-rows-always/#findComment-1053293 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.