GROUP BY partial field

Thread Solved

Join Date: Oct 2004
Posts: 29
Reputation: trashed is an unknown quantity at this point 
Solved Threads: 0
trashed trashed is offline Offline
Light Poster

GROUP BY partial field

 
0
  #1
Feb 1st, 2009
Hi all.
I have a table with a this records:

  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):

  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
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 8
Reputation: supportresort is an unknown quantity at this point 
Solved Threads: 2
supportresort supportresort is offline Offline
Newbie Poster

Re: GROUP BY partial field

 
1
  #2
Feb 1st, 2009
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:

  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.
Support Resort
http://www.supportresort.com
Bringing offshore expertise to the world
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 29
Reputation: trashed is an unknown quantity at this point 
Solved Threads: 0
trashed trashed is offline Offline
Light Poster

Re: GROUP BY partial field

 
0
  #3
Feb 2nd, 2009
kudos!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the MySQL Forum


Views: 752 | Replies: 2
Thread Tools Search this Thread



Tag cloud for MySQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC