mysql关联查询并得到某条数据的排名。
现在有一个订单表,表中有一字段shop_id(店铺id),还有一个shop表(店铺表),现在我想得到某个店铺所有订单总额在所有店铺里面的排名,这个sql语句该怎么写呢?或者说有什么简单的办法去实现呢?
ps:店铺表中没有销售总额字段,我现在是先将所有店铺的销售额进行排序,但是如果去遍历数组比较某个店铺的销售额的排名实在是太麻烦了
回复内容:
现在有一个订单表,表中有一字段shop_id(店铺id),还有一个shop表(店铺表),现在我想得到某个店铺所有订单总额在所有店铺里面的排名,这个sql语句该怎么写呢?或者说有什么简单的办法去实现呢?
ps:店铺表中没有销售总额字段,我现在是先将所有店铺的销售额进行排序,但是如果去遍历数组比较某个店铺的销售额的排名实在是太麻烦了
<code>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</code>
亲测,可以实现题主的需求
麻烦的很,最佳办法就是表里增加销售总额字段,不然的话每次要排名就要统计一次,数据库肯定挂。这种应用肯定是你进店铺的时候显示一个排名信息,N个人进不同的店铺,你要统计N次每个店铺的销售总额吗?
正解是增加销售总额和排名字段,使用定时任务每天更新一次。
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
简单想了下,两个思路
一是先算出这个店铺的销售额是多少,然后去所有店铺销售额的结果集中找大于这个值的count数
二是引入预定义用户变量来实现,set @mycnt = 0; select (@mycnt := @mycnt + 1) as ROWNUM
另外如果这个总排名很常用的话,还是建议建立一个总额的字段

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

How to connect to MySQL using phpMyAdmin? The URL to access phpMyAdmin is usually http://localhost/phpmyadmin or http://[your server IP address]/phpmyadmin. Enter your MySQL username and password. Select the database you want to connect to. Click the "Connection" button to establish a connection.
