Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Optimalisation of MySQL query

Posted on 2008-10-08
1
209 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 143

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 count occurrences of each item in an array.

790 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