Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-10-2013, 04:05 PM   PM User | #1
mbarandao
Regular Coder

 
Join Date: Nov 2010
Location: Washington DC
Posts: 311
Thanks: 19
Thanked 1 Time in 1 Post
mbarandao is an unknown quantity at this point
count inserted records for a specific day of the week

Hello all:

I would like to query my mysql table for a total number of records inserted during a specified day of the week within a specified time period.

More clearly: count number records inserted on Tuesdays during the time period of 2013-01-31 - 2013-02-03. The first day of the week being Monday.

My date field in mysql is formated as 0000-00-00 00:00:00

Any thoughts on this is appreciated

Best,
Mossa
mbarandao is offline   Reply With Quote
Old 02-11-2013, 12:31 AM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
what have you tried? you can specify DAYOFWEEK(yourdatefield) to get the day of week from the date or datetime entered.
guelphdad is offline   Reply With Quote
Old 02-11-2013, 02:20 AM   PM User | #3
mbarandao
Regular Coder

 
Join Date: Nov 2010
Location: Washington DC
Posts: 311
Thanks: 19
Thanked 1 Time in 1 Post
mbarandao is an unknown quantity at this point
Thanks for the reply. I have tried the following:

PHP Code:
select count(*) from history
where timeentry between 
"2012-02-03" and "2012-02-09" 
and dayofweek(timeentry) = 
However, I would like to specify that my beginning day of the week is Monday so therefore, if I'm looking for all of the visits that occurred on "Tuesdays" for the "past month", Tuesday would be represented by 2. Is this thinking correct?

Additionally, I really need the above statement to get the Average visits for Tuesdays for the past specified time period. My statement above does not really give me the average. So essentially, I need some guidance in that area as well.

Any further thoughts on this is appreciated!
Mossa

Last edited by mbarandao; 02-11-2013 at 02:27 PM..
mbarandao is offline   Reply With Quote
Old 02-11-2013, 07:20 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
MySQL doesn't support different days for the beginning of the week (some other databases and languages do, but not MySQL). So you would have to use the MySQL standard, no choice: 0 is Monday, 1 is Tuesday, etc., 6 is Sunday.

Average visits for Tuesday in (say) 2013:
Code:
SELECT AVG(X.visits)
FROM ( SELECT DATE(timeentry), COUNT(*) AS visits
       FROM history
       WHERE YEAR(timeentry) = 2013
       AND WEEKDAY(timeentry) = 1 ) AS X
Average visits for Thursday for last 3 months:
Code:
SELECT AVG(X.visits)
FROM ( SELECT DATE(timeentry), COUNT(*) AS visits
       FROM history
       WHERE timeentry >= DATE_SUB( CURDATE(), INTERVAL 3 MONTH) 
       AND WEEKDAY(timeentry) = 3 ) AS X
And so on.

Learn to use the builtin functions the MySQL gives you. Read the manual!!!
http://dev.mysql.com/doc/refman/5.5/...functions.html
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
mbarandao (02-12-2013)
Old 02-11-2013, 09:57 PM   PM User | #5
mbarandao
Regular Coder

 
Join Date: Nov 2010
Location: Washington DC
Posts: 311
Thanks: 19
Thanked 1 Time in 1 Post
mbarandao is an unknown quantity at this point
Old Pedant, I appreciate your thoughts on this. Once again, you have come to my aide. I'm much appreciative. I will read more on the link you provided. Thanks again!

Best,
Mossa
mbarandao is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:20 AM.


Advertisement
Log in to turn off these ads.