Jump to content

Return 7 rows ALWAYS


The Little Guy

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'

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.