Home >Database >Mysql Tutorial >Maximum number of columns per group in MySQL

Maximum number of columns per group in MySQL

WBOY
WBOYforward
2023-08-27 23:13:021459browse

MySQL 中每组的最大列数

Let us understand how to find the maximum value of each set of columns in MySQL -

SELECT colName1, MAX(colName2)
FROM tableName
GROUP BY colName1
ORDER BY colName1;

We will now see an example. Suppose we have a table PRODUCT -

+---------+--------+
| Article | Price  |
+---------+--------+
| 1       | 255.50 |
| 1       | 256.05 |
| 2       | 90.50  |
| 3       | 120.50 |
| 3       | 123.10 |
| 3       | 122.10 |
+---------+--------+

The following is the query to get the maximum number of columns per group-

Query

SELECT Article, MAX(Price) AS MaxPrice
FROM Product
GROUP BY Article
ORDER BY Article;

Output

+--------------+--------------+
| Article      | MaxPrice     |
+--------------+--------------+
| 0001         | 256.05       |
| 0002         | 90.50        |
| 0003         | 123.10       |
+--------------+--------------+

The above is the detailed content of Maximum number of columns per group in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete