Now there is an order table with a field shop_id (store ID) and a shop table (store table), and now I want to get the rank of all the orders in a store in all the stores, how can I write this SQL statement? Or is there any simple way to achieve it?
PS: There is no sales total field in the store table, I am now sorting out the sales for all stores, but it is too troublesome to go through the array to compare the sales of a store.
Reply content:
Now there is an order table with a field shop_id (store ID) and a shop table (store table), and now I want to get the rank of all the orders in a store in all the stores, how can I write this SQL statement? Or is there any simple way to achieve it?
PS: There is no sales total field in the store table, I am now sorting out the sales for all stores, but it is too troublesome to go through the array to compare the sales of a store.
SELECT a.id,a.shopname,sum(b.order_price) FROM tb_shop a LEFT JOIN tb_item_order b on a.id=b.shop_id GROUP BY a.id ORDER BY sum(b.order_price) desc
Pro-Test, can achieve the needs of the main problem
Trouble is, the best way is to increase the amount of sales in the table field, or every time to rank to be counted once, the database must hang. This application must be when you enter the store to display a ranking information, n people into different stores, you want to count n times each store sales total?
The positive solution is to increase sales totals and row name fields, using Scheduled tasks to update once a day.
SELECT a.id, sum(b.price) as total_price FROM shop a, order b WHERE a.id = b.shop_id GROUP BY a.id ORDER BY total_price desc
Simple thought, two ideas
One is to figure out how much the store's sales are, and then go to the results of all store sales to find the count number greater than this value
Second, the introduction of predefined user variables to achieve, set @mycnt = 0; Select (@mycnt: = @mycnt + 1) as ROWNUM
In addition, if this general ranking is very common, it is recommended to create a total of the field