Solved

Optimalisation of MySQL query

Posted on 2008-10-08
1
207 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
ID: 22667654
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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.

777 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