Solved

MySQL Query For Day of Week

Posted on 2009-04-13
7
479 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 200 total points
ID: 24130171

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

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

713 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