S
S
Shuhratjon Djumaev2016-07-04 15:05:44
MySQL
Shuhratjon Djumaev, 2016-07-04 15:05:44

How to combine these sql queries?

Queries to be merged:

select SUM(sum) AS "monthsum" from bills where YEAR(`date`) = YEAR(NOW()) AND MONTH(`date`) = MONTH(NOW())

select SUM(sum) AS "weeksum" from bills where YEAR(`date`) = YEAR(NOW()) AND WEEK(`date`, 1) = WEEK(NOW(), 1)

select SUM(sum) AS "daysum" from bills where DATE_FORMAT(date, "%Y-%m-%d") = CURDATE()

You need to combine them according to the rules of sql queries so that the query is easy.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SharuPoNemnogu, 2016-07-04
@shuhratjon

more or less like this.

SELECT SUM(CASE WHEN MONTH('date') = MONTH(NOW()) THEN sum END) AS 'monthsum',
       SUM(CASE WHEN WEEK('date', 1) = WEEK(NOW(), 1) THEN sum END) AS 'weeksum',
       SUM(CASE WHEN DATE_FORMAT(date, '%Y-%m-%d') = CURDATE() THEN sum END) AS 'daysum'
FROM bills
WHERE YEAR('date') = YEAR(NOW());

ps I do not advise you to call the fields reserved names

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question