D
D
Denis Ogurtsov2014-06-03 12:36:23
MySQL
Denis Ogurtsov, 2014-06-03 12:36:23

How to write such a query to the table?

There is a forms table
id count_forms
1 800
2 900
3 10
4 60
5 596
You need to make the following query:
Select id from the forms table where SUM(count_forms) is greater than 1000.
BUT the sum should be closest to 1000.

SELECT id, SUM(count_forms) as sum_count_forms FROM forms WHERE sum_count_forms>1000

does not work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rpsl, 2014-06-03
@Rpsl

Use HAVING.
More or less like this:

SELECT id, SUM(count_forms) as sum_count_forms 
FROM forms 
HAVING sum_count_forms > 1000
ORDER BY sum_count_forms ASC
LIMIT 1

F
Fedor Kolov, 2014-06-03
@d_f

Is the task set correctly? If we have one table, then the sum function will simply sum all the values ​​\u200b\u200band give the final amount. And then it will be compared with 1000. Is there a grouping in the table?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question