MySQL associates a query and gets the rank of a piece of data.

Source: Internet
Author: User
Keywords Php mysql
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

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.