SELECT count(item_id) as today FROM table1
WHERE DATE(creation_date) > DATE(NOW() - INTERVAL 1 DAY);
SELECT count(item_id) as yesterday from table1
where DATE(creation_date) = DATE(NOW() - INTERVAL 1 DAY);
SELECT count(item_id) year_period from table1
where creation_date > now() - interval 1 year;
I have the following queries, I would like to get the results returned from these queries in one row. as shown below:
----------------------------------------
|today | yesterday | one_year_period |
----------------------------------------
| 2 | 5 | 7 |
----------------------------------------
I am award of a few way to accompliish this but I would like a more effecient solution to accomplish what I want. I can accompliish what I want by using a union as seen here
SELECT count(item_id) as today FROM table1
WHERE DATE(creation_date) > DATE(NOW() - INTERVAL 1 DAY)
union
SELECT count(item_id) as yesterday from table1
where DATE(creation_date) = DATE(NOW() - INTERVAL 1 DAY)
union
SELECT count(item_id) year_period from table1
where creation_date > now() - interval 1 year;
I would like to optimize my solution so that the query is not repeated on 3 seperate occasions.
SUM(CASE())I think is what you are looking for.DATE(NOW() - INTERVAL 1 DAY)can be simplified toCURDATE() - INTERVAL 1 DAY.