This question has already been answered:

Good afternoon dear experts, help with a question on MySQL. Suppose there is a table "employees", in it the columns "id (employee)", "salary", "payment date" how to get the sum of the last 10 payments (by date) of this table? thank.

SELECT SUM(зарплата) FROM сотрудники "что дописать?" 

Reported as a duplicate by members Mike ,, Kromster , 0xdb , Arsen on Feb 15 '18 at 12:03 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • and any columns like id or date are? - Peresada
  • 2
    What are the "recent" entries? In SQL, there is no concept of "first" and "last" until the sort order is explicitly set - Mike
  • @Peresada could have attached a table schema, you painted a not very clear question, wrote it literally - "The sum of the last 10 table entries", but the answer with a sample of the last 10 entries began to be minus and criticize. Refine and rephrase the question - Sasha Borichevsky
  • @ SashaBorichevsky You apparently don’t address this, Igor Salamov asked the question, Peresada hardly knows the structure of the tables and so on, because it’s not his question ... - Mike
  • @Mike, I apologize, did not see) - Sasha Borichevsky

2 answers 2

Something like this:

 select sum(зарплата) from (select зарплата from сотрудники order by date desc limit 10) 
     SELECT SUM(`зарплата`) FROM `сотрудики` ORDER BY `date` DESC LIMIT 10 
    • one
      ORDER BY and LIMIT are triggered after GROUP BY. And the presence of the SUM () function conducts an implicit group by. your query will return the sum of all rows in the table - Mike
    • desc limit 10 will display the sum of all entries, because this limit is the limit of rows to output, and sum is just one row, that is, even limit 1, will output the sum of all rows of the table - Igor Salamov
    • well then get the data in the php code sum them - terzik993