943,899 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 1134
  • MySQL RSS
Feb 1st, 2009
0

GROUP BY partial field

Expand Post »
Hi all.
I have a table with a this records:

MySQL Syntax (Toggle Plain Text)
  1. <!-- Table user_activity -->
  2. <user_activity>
  3. <id>1</id>
  4. <event>LOGIN</event>
  5. <username>user@domain.com</username>
  6. <domain>domain.com</domain>
  7. <remote_address>ip_address</remote_address>
  8. <date>2008-05-23 20:19:36</date>
  9. <comments></comments>
  10. </user_activity>

I am trying to query the DB to get the total results of activity PER DAY. This is my query (which obviously doesn't work):

MySQL Syntax (Toggle Plain Text)
  1. SELECT DATE, COUNT(*) FROM `user_activity` WHERE `event` LIKE "LOGIN" AND `DATE` LIKE "2008-05%" GROUP BY DATE

Since the date field has also a hour timestamp, I get thousands of results with count 1.
Is there any way to group the results only by the first 10 characters (and omit the time of day)?
Thanks
Reputation Points: 11
Solved Threads: 0
Light Poster
trashed is offline Offline
30 posts
since Oct 2004
Feb 1st, 2009
1

Re: GROUP BY partial field

MySQL has a DATE function to extract only the date from a DATETIME field. You should be able to use a query such as the following:

sql Syntax (Toggle Plain Text)
  1. SELECT DATE(DATE), count(*) FROM user_activity WHERE event LIKE "LOGIN" GROUP BY DATE(DATE);

I hope that helps.
Last edited by peter_budo; Feb 2nd, 2009 at 7:12 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 15
Solved Threads: 2
Newbie Poster
supportresort is offline Offline
8 posts
since Jan 2009
Feb 2nd, 2009
0

Re: GROUP BY partial field

kudos!
Reputation Points: 11
Solved Threads: 0
Light Poster
trashed is offline Offline
30 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: MY SQL reports
Next Thread in MySQL Forum Timeline: Please suggest: MsAccess to mysql





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC