Home > Database > Mysql Tutorial > body text

How can we use group function with non-group fields in MySQL SELECT query?

WBOY
Release: 2023-09-10 17:41:02
forward
490 people have browsed it

我们如何在 MySQL SELECT 查询中将组函数与非组字段一起使用?

If we want to use grouping function on non-grouped fields in SELECT query, we must use GROUP BY clause. The general syntax can be as follows

Syntax

SELECT group_function1,…, non-group-column1,… from table_name GROUP BY column_name;
Copy after login

Example

mysql> Select COUNT(*), id from Student GROUP BY id;
+----------+------+
| COUNT(*) | id   |
+----------+------+
| 1        | 1    |
| 1        | 2    |
| 1        | 15   |
| 1        | 17   |
| 1        | 20   |
+----------+------+
5 rows in set (0.00 sec)

mysql> Select COUNT(*), address from Student GROUP BY id;
+----------+---------+
| COUNT(*) | address |
+----------+---------+
| 1        | Delhi   |
| 1        | Mumbai  |
| 1        | Delhi   |
| 1        | Shimla  |
| 1        | Jaipur  |
+----------+---------+
5 rows in set (0.00 sec)
Copy after login

The fields after the GROUP BY clause can be different from the non-group fields given in the SELECT query.

The above is the detailed content of How can we use group function with non-group fields in MySQL SELECT query?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template