Solved

MySQL Query For Day of Week

Posted on 2009-04-13
7
483 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 total points
ID: 24130171

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

Open in new window

0
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
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 300 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 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