Solved

Optimalisation of MySQL query

Posted on 2008-10-08
1
202 Views
Last Modified: 2013-12-12
I have an mysql table with jobs. This table contains an entry 'insertionDate' which contains the date that a job was insertion.
Now I want to know the amount of jobs that were
- inserted today
- inserted this week
- inserted this month.

How can I do this with only 1 query?

Now I use 3 queries for this. The query for the jobs from last week is:
SELECT

  COUNT( * )

FROM

 jobs

WHERE

 DATEDIFF( NOW(), insertionDate ) < 7

Open in new window

0
Comment
Question by:thijs321
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
please check out this:
SELECT count(*) inserted_this_month
 , SUM(case when insertionDate >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) then 1 else 0 end) inserted_last_7_days
 , SUM(case when insertionDate >= CURDATE then 1 else 0 end) inserted_today
FROM jobs
WHERE insertionDate >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH) 

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

763 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

12 Experts available now in Live!

Get 1:1 Help Now