Solved

Optimalisation of MySQL query

Posted on 2008-10-08
1
212 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
[X]
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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 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