INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Return previous days entries

Return previous days entries

(OP)
Hi

I have been trying to run a query to return all the previous days entries. I cant use interval - 1 day because lets say the last entry was on friday and I am running the query on monday, it would return 0 results.

This is the select statement I have so far:
select * from timesheet where date(dateTimeStamp) = date(now()) order by dateTimeStamp desc;

I am out of ideas how to manipulate it. Any ideas appreciated.

Many thanks

RE: Return previous days entries

(OP)
Hi, I think I have found the answer on the mysql site, thought I would post it here just in case someone else needs it.

In most cases you need results from yesterday, but for Sunday and Monday we
must correct extra for the weekend.

DAYOFWEEK() returns 1 for Sunday, 2 for Monday, etc., so we can use this to
make a correction value:

SET @COR = (DAYOFWEEK(NOW()) < 3) * DAYOFWEEK(NOW());
SELECT * FROM table WHERE `datetime` > CURDATE() - INTERVAL (1 + @COR) DAY
AND `datetime` < CURDATE() - INTERVAL @COR DAY;

This construction is index-friendly, because MySQL will reduce the
calculation to a constant before running the query. The result is a simple
col > constant expression which can make use of indexes if they are present.
Expressions such as TO_DAYS(`datetime`) (as suggested by you) will result in
a full table scan and slow down the query significantly.

If you consider Saturday as a business day you only need to make a
correction for Monday:

SET @COR = (DAYOFWEEK(NOW()) = 2);

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close