Solved

How do I select arbitrary date ranges in a MySQL query

Posted on 2009-07-15
3
297 Views
Last Modified: 2013-12-13
I need a query/code snippet that would allow me to get a date-range from the previous Friday night until now.  I have figured out how to get date results from the last 7 days, but I need them specifically from last Friday.

On a slightly different note, but related to the same project, I need some code that would allow me to get results from each disparate month of the 12 months, starting with a year ago.  For example, I need to return the number of orders within dates of July 1st - the 31st of 2008, August 1st - 31st of 2008, ... up to July 1st - 31st of 2009.  This data will be fed into a dynamically created chart, so it has to all move along dynamically with the current date.
0
Comment
Question by:russianryebread
  • 3
3 Comments
 
LVL 14

Accepted Solution

by:
racek earned 125 total points
Comment Utility
weekday 4 is friday ...

SELECT *
FROM  your_table
WHERE you_date_column > (SELECT  DATE_SUB(MAX(you_date_column), INTERVAL 7 days)
                                             FROM your_table  where WEEKDAY(you_date_column) = 3 )  ;
0
 
LVL 14

Expert Comment

by:racek
Comment Utility
or faster solution ...
SELECT *

FROM  your_table

WHERE LEFT(your_date_column,10) 

    = LEFT(CASE WEEKDAY(NOW()) 

   WHEN 4 THEN DATE_SUB(your_date_column, INTERVAL 7 days)

   WHEN 5 THEN DATE_SUB(your_date_column, INTERVAL 8 days)

   WHEN 6 THEN DATE_SUB(your_date_column, INTERVAL 9 days)

   WHEN 3 THEN DATE_SUB(your_date_column, INTERVAL 6 days)

   WHEN 2 THEN DATE_SUB(your_date_column, INTERVAL 5 days)

   WHEN 1 THEN DATE_SUB(your_date_column, INTERVAL 4 days)

   WHEN 0 THEN DATE_SUB(your_date_column, INTERVAL 3 days)

END , 10);

Open in new window

0
 
LVL 14

Assisted Solution

by:racek
racek earned 125 total points
Comment Utility
second one - use

select
YEAR(your_date_column) as year
MONTH(your_date_column) as month,
SUM(datacol) as datasum
FROM your_table
GROUP by 1,2
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Creating and Managing Databases with phpMyAdmin in cPanel.
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…
The viewer will learn how to dynamically set the form action using jQuery.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now