search
HomeDatabaseMysql TutorialMySQL中的WITH ROLLUP

MySQL的扩展SQL中有一个非常有意思的应用WITH ROLLUP,在分组的统计数据的基础上再进行相同的统计(SUM,AVG,COUNThellip;),非

MySQL的扩展SQL中有一个非常有意思的应用WITH ROLLUP,,在分组的统计数据的基础上再进行相同的统计(SUM,AVG,COUNT…),非常类似于Oracle中统计函数的功能,Oracle的统计函数更多更强大。

下面演示单个司机以及所有司机的总行驶里程数和平均行驶里程数:

mysql> select name,sum(miles) as 'miles/driver'

    -> from driver_log group by name with rollup;

+-------+--------------+

| name  | miles/driver |

+-------+--------------+

| Ben  |          362 |

| Henry |          911 |

| Suzi  |          893 |

| NULL  |        2166 |

+-------+--------------+

4 rows in set (0.00 sec)

 

mysql> select name,avg(miles) as driver_avg

    -> from driver_log group by name with rollup;

+-------+------------+

| name  | driver_avg |

+-------+------------+

| Ben  |  120.6667 |

| Henry |  182.2000 |

| Suzi  |  446.5000 |

| NULL  |  216.6000 |

+-------+------------+

4 rows in set (0.00 sec)

 

mysql> select name,sum(miles) as 'miles/driver',avg(miles) as driver_avg

    -> from driver_log group by name with rollup;

+-------+--------------+------------+

| name  | miles/driver | driver_avg |

+-------+--------------+------------+

| Ben  |          362 |  120.6667 |

| Henry |          911 |  182.2000 |

| Suzi  |          893 |  446.5000 |

| NULL  |        2166 |  216.6000 |

+-------+--------------+------------+

4 rows in set (0.00 sec)

在多个分组下WITH ROLLUP同样有效:

mysql> select srcuser,dstuser,count(*) from mail group by srcuser,dstuser;

+---------+---------+----------+

| srcuser | dstuser | count(*) |

+---------+---------+----------+

| barb    | barb    |        1 |

| barb    | tricia  |        2 |

| gene    | barb    |        2 |

| gene    | gene    |        3 |

| gene    | tricia  |        1 |

| phil    | barb    |        1 |

| phil    | phil    |        2 |

| phil    | tricia  |        2 |

| tricia  | gene    |        1 |

| tricia  | phil    |        1 |

+---------+---------+----------+

10 rows in set (0.05 sec)

 

mysql> select srcuser,dstuser,count(*) from mail group by srcuser,dstuser with rollup;

+---------+---------+----------+

| srcuser | dstuser | count(*) |

+---------+---------+----------+

| barb    | barb    |        1 |

| barb    | tricia  |        2 |

| barb    | NULL    |        3 |

| gene    | barb    |        2 |

| gene    | gene    |        3 |

| gene    | tricia  |        1 |

| gene    | NULL    |        6 |

| phil    | barb    |        1 |

| phil    | phil    |        2 |

| phil    | tricia  |        2 |

| phil    | NULL    |        5 |

| tricia  | gene    |        1 |

| tricia  | phil    |        1 |

| tricia  | NULL    |        2 |

| NULL    | NULL    |      16 |

+---------+---------+----------+

15 rows in set (0.00 sec)

本文永久更新链接地址

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does MySQL handle concurrency compared to other RDBMS?How does MySQL handle concurrency compared to other RDBMS?Apr 29, 2025 am 12:44 AM

MySQLhandlesconcurrencyusingamixofrow-levelandtable-levellocking,primarilythroughInnoDB'srow-levellocking.ComparedtootherRDBMS,MySQL'sapproachisefficientformanyusecasesbutmayfacechallengeswithdeadlocksandlacksadvancedfeatureslikePostgreSQL'sSerializa

How does MySQL handle transactions compared to other relational databases?How does MySQL handle transactions compared to other relational databases?Apr 29, 2025 am 12:37 AM

MySQLhandlestransactionseffectivelyusingtheInnoDBengine,supportingACIDpropertiessimilartoPostgreSQLandOracle.1)MySQLusesREPEATABLEREADasthedefaultisolationlevel,whichcanbeadjustedtoREADCOMMITTEDforhigh-trafficscenarios.2)Itoptimizesperformancewithabu

What are the data types available in MySQL?What are the data types available in MySQL?Apr 29, 2025 am 12:28 AM

MySQL data types are divided into numerical, date and time, string, binary and spatial types. Selecting the correct type can optimize database performance and data storage.

What are some best practices for writing efficient SQL queries in MySQL?What are some best practices for writing efficient SQL queries in MySQL?Apr 29, 2025 am 12:24 AM

Best practices include: 1) Understanding the data structure and MySQL processing methods, 2) Appropriate indexing, 3) Avoid SELECT*, 4) Using appropriate JOIN types, 5) Use subqueries with caution, 6) Analyzing queries with EXPLAIN, 7) Consider the impact of queries on server resources, 8) Maintain the database regularly. These practices can make MySQL queries not only fast, but also maintainability, scalability and resource efficiency.

How does MySQL differ from PostgreSQL?How does MySQL differ from PostgreSQL?Apr 29, 2025 am 12:23 AM

MySQLisbetterforspeedandsimplicity,suitableforwebapplications;PostgreSQLexcelsincomplexdatascenarioswithrobustfeatures.MySQLisidealforquickprojectsandread-heavytasks,whilePostgreSQLispreferredforapplicationsrequiringstrictdataintegrityandadvancedSQLf

How does MySQL handle data replication?How does MySQL handle data replication?Apr 28, 2025 am 12:25 AM

MySQL processes data replication through three modes: asynchronous, semi-synchronous and group replication. 1) Asynchronous replication performance is high but data may be lost. 2) Semi-synchronous replication improves data security but increases latency. 3) Group replication supports multi-master replication and failover, suitable for high availability requirements.

How can you use the EXPLAIN statement to analyze query performance?How can you use the EXPLAIN statement to analyze query performance?Apr 28, 2025 am 12:24 AM

The EXPLAIN statement can be used to analyze and improve SQL query performance. 1. Execute the EXPLAIN statement to view the query plan. 2. Analyze the output results, pay attention to access type, index usage and JOIN order. 3. Create or adjust indexes based on the analysis results, optimize JOIN operations, and avoid full table scanning to improve query efficiency.

How do you back up and restore a MySQL database?How do you back up and restore a MySQL database?Apr 28, 2025 am 12:23 AM

Using mysqldump for logical backup and MySQLEnterpriseBackup for hot backup are effective ways to back up MySQL databases. 1. Use mysqldump to back up the database: mysqldump-uroot-pmydatabase>mydatabase_backup.sql. 2. Use MySQLEnterpriseBackup for hot backup: mysqlbackup--user=root-password=password--backup-dir=/path/to/backupbackup. When recovering, use the corresponding life

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.