Solved

MySQL Query For Day of Week

Posted on 2009-04-13
7
485 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

615 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