Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL Query For Day of Week

Posted on 2009-04-13
7
Medium Priority
?
496 Views
Last Modified: 2013-12-12
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.

0
Comment
Question by:DVation191
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Expert Comment

by:joshbenner
ID: 24130130
If you're building the query in PHP, then you just query for the data you need:

SELECT text FROM table WHERE date = '2009-04-13'

In PHP, your code might resemble:
<?php
$sql = 'SELECT text FROM table WHERE date = \''.date('Y-m-d').'\'';
$result = mysql_query($sql);
?>

Open in new window

0
 
LVL 14

Expert Comment

by:racek
ID: 24130159
SELECT WEEKDAY(date) , xxxx
from yourTable
WHERE WEEK(date) = WEEK/NOW())
0
 
LVL 14

Assisted Solution

by:racek
racek earned 800 total points
ID: 24130171

oops
 
SELECT WEEKDAY(date) , xxxx
from yourTable
WHERE WEEK(date) = WEEK(NOW())

Open in new window

0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 20

Author Comment

by:DVation191
ID: 24130301
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.
0
 
LVL 1

Accepted Solution

by:
joshbenner earned 1200 total points
ID: 24130353

<?php
$now = time();
$currentDOW = date('w', $now);
$wantedDOW = 3; // 0 = sunday, 6 = saturday
$diff = $wantedDOW - $currentDOW;
$date = date('Y-m-d', strtotime("$diff day", $now));
$sql = "SELECT text FROM table WHERE date = '$date'";

Open in new window

0
 
LVL 14

Expert Comment

by:racek
ID: 24130384
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())
0
 
LVL 20

Author Comment

by:DVation191
ID: 24131452
Although you both answered the question, I ended up using joshbenner's snippet to achieve my goal. Thank you both.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question