When python operates mysql, how can it be sorted according to the size of the group summation after grouping and summing first?

The structure of the

table is a top-up record table. Users generate a record for each top-up. I want to sum each user"s top-up within a period of time, sort it according to the total amount of each user"s top-up, and intercept the first few bits. The current sql statement is as follows:

clipboard.png

but there is no intercept operation, sorting is not the order_by, of sql because it needs to be sorted according to the sum, now I want to realize the requirement through the sql statement and find the code. (the reason why you want to optimize is that too much data will cause sql statements to slow down, right? Or is the impact less significant, but will it slow down if the back-end is transferred from the front end to the front end? )

Aug.01,2021

SQLAlchemy is essentially no different from handwritten SQL statements, because SQLAlchemy also generates SQL statements and passes them to the database. Performance problems will not be greatly improved due to handwriting, while SQLAlchemy can avoid some common errors and is easy to reconstruct and modify. As for performance problems, it is best to solve them by designing for requirements, such as creating a separate statistical table to modify the total and balance at the same time as users recharge and consume, rather than real-time statistics each time.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b22856-2b607.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b22856-2b607.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?