Jump to content

Group multiple by month


doddsey_65

Recommended Posts

It seems so simple but i have spent the best part of 2 hours trying to do this. I want to select all the topics from the database and all of the users, then i want to display the dates of the last 7 days and show how many users registered and how many topics were created on that day like so:

 

4th Aug

2 topics

1 user

 

3rd Aug

5 topics

3 Users

 

and so on.

 

Anyone have any tips or can anyone help?

 

Link to comment
Share on other sites

UNIX timestamp

 

MySQL does not have a unix timestamp type field. It has "DATETIME", "DATE", and "TIMESTAMP" (which is not a unix timestamp). If you are simply storing a unix timestamp value from PHP into a numeric type field in MySQL you cannot group by dates without more work - if at all. It *may* be possible using MySQL's FROM_UNIXTIME, but I've never tried it and don't know if you can use MySQL's other data functions on top of that one.

 

So, please be specific in the exact field type you are using to store the date value in MySQL.

Link to comment
Share on other sites

Hi

 

Something like this should do it (not tested, so probably some typos)

 

SELECT Last7Days.aDay, COUNT(topics.id), COUNT(users.registerdate)
FROM (SELECT DATE_ADD(NOW(), INTERVAL (-1*i) DAY) AS aDay FROM integers WHERE i BETWEEN 0 AND 6) Last7Days
LEFT OUTER JOIN topics ON Last7Days.aDay = DATE(FROM_UNIXTIME(topics.topicDate))
LEFT OUTER JOIN users ON Last7Days.aDay = DATE(FROM_UNIXTIME(users.registerdate))
GROUP BY Last7Days.aDay

 

Relies on a table of integers from 0 to 9.

 

All the best

 

Keith

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.