Home > Database > Mysql Tutorial > How can we create a MySQL view using GROUP BY clause?

How can we create a MySQL view using GROUP BY clause?

WBOY
Release: 2023-09-02 12:49:02
forward
1499 people have browsed it

我们如何使用 GROUP BY 子句创建 MySQL 视图?

We can use GROUP BY to group the values ​​in a column and if required, we can perform calculations on the column. You can use functions like COUNT, SUM, AVG, etc. on grouping columns. To understand the GROUP BY clause with a view, we create a view named "Info" using the base table "Student_info" with the following data -

mysql> Select * from Student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Chandigarh | Literature |
| 125  | Raman   | Shimla     | Computers  |
| 130  | Ram     | Jhansi     | Computers  |
| 132  | Shyam   | Chandigarh | Economics  |
| 133  | Mohan   | Delhi      | Computers  |
+------+---------+------------+------------+
6 rows in set (0.00 sec)
Copy after login

grammar

Create or Replace View view_name AS Select_statements FROM table GROUP BY expression1, expression2, ... expression_n;
Copy after login

Example

mysql> Create or Replace View Info AS select Subject, COUNT(*) FROM Student_info GROUP BY Subject;
Query OK, 0 rows affected (0.10 sec)

mysql> Select * from info;
+------------+----------+
| Subject    | COUNT(*) |
+------------+----------+
| Computers  |    3     |
| Economics  |    1     |
| History    |    1     |
| Literature |    1     |
+------------+----------+
4 rows in set (0.00 sec)

mysql> Create or Replace View Info AS select Subject,Name, COUNT(Subject) FROM Student_info GROUP BY Subject, Name;
Query OK, 0 rows affected (0.05 sec)
Copy after login

The above query will contain two columns in the GROUP BY clause.

mysql> Select * from info;
+------------+---------+----------------+
| Subject    | Name    | COUNT(Subject) |
+------------+---------+----------------+
| Computers  | Mohan   |       1        |
| Computers  | Ram     |       1        |
| Computers  | Raman   |       1        |
| Economics  | Shyam   |       1        |
| History    | YashPal |       1        |
| Literature | Gaurav  |       1        |
+------------+---------+----------------+
6 rows in set (0.00 sec)
Copy after login

The above result set shows that the GROUP BY clause groups a set of rows into a set of summary rows by the value of the column.

The above is the detailed content of How can we create a MySQL view using GROUP BY clause?. 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