I need to get the sum of 10 lines, for this I try to use the following query:

SELECT SUM(hosts) FROM traffic LIMIT 10 

I get for some reason the mousse of all the records ...

How can I get the amount of only 10 entries?

    2 answers 2

    1. set the limit on the number of lines in the where part:

    SELECT SUM (hosts) FROM traffic WHERE condition for the selection of 10 lines

    1. through subquery

    SELECT SUM (hosts) FROM (SELECT hosts FROM traffic ORDER BY поле, по которому сортировка LIMIT 10) t

    • The problem is just that without WHERE, the second option with sub queries is what you need, ORDER BY by itself. Thank you very much for suggesting! - Yuri Zavada

    Try:

     SELECT SUM(hosts) FROM (SELECT * FROM traffic LIMIT 10) AS tables