DVation191
asked on
MySQL Query For Day of Week
Given a MySQL Table fields for date[DATE] and text[VARCHAR(255)], how can I formulate a MySQL query to get all the text of a day on the current week.
For example, Monday is the start of the week. I want to get all the data for monday. I then want to get the data for Tuesday (which would return nothing, because there hasn't been any data entered for tomorrow yet). And so on for the rest of the current week.
For example, Monday is the start of the week. I want to get all the data for monday. I then want to get the data for Tuesday (which would return nothing, because there hasn't been any data entered for tomorrow yet). And so on for the rest of the current week.
SELECT WEEKDAY(date) , xxxx
from yourTable
WHERE WEEK(date) = WEEK/NOW())
from yourTable
WHERE WEEK(date) = WEEK/NOW())
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The problem with selecting it by date explicitly is that I'll be dynamically displaying content using PHP and the "day of the week" will change week to week.
@racek, correct me if I'm wrong, but won't that just select all of today's data? I need all the data for the current week.
@racek, correct me if I'm wrong, but won't that just select all of today's data? I need all the data for the current week.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
WEEK(NOW()) ... this week
WEEK(date) ... week of date in tatabase ... date is column name
query returns all records from this week :-)
SELECT WEEKDAY(date) , xxxx
from yourTable
WHERE WEEK(date) = WEEK(NOW())
WEEK(date) ... week of date in tatabase ... date is column name
query returns all records from this week :-)
SELECT WEEKDAY(date) , xxxx
from yourTable
WHERE WEEK(date) = WEEK(NOW())
ASKER
Although you both answered the question, I ended up using joshbenner's snippet to achieve my goal. Thank you both.
SELECT text FROM table WHERE date = '2009-04-13'
In PHP, your code might resemble:
Open in new window