Select*fromStudent_info;+------+----- -- --+------------+------------+|id|Nama|Alamat|Subjek|+------+-- -- -----"/> Select*fromStudent_info;+------+----- -- --+------------+------------+|id|Nama|Alamat|Subjek|+------+-- -- -----">
Rumah > Artikel > pangkalan data > Bagaimanakah kita boleh membuat paparan MySQL menggunakan klausa GROUP BY?
Kita boleh menggunakan GROUP BY untuk mengumpulkan nilai dalam lajur dan jika perlu kita boleh melakukan pengiraan pada lajur. Anda boleh menggunakan fungsi seperti COUNT, SUM, AVG, dsb. pada mengumpulkan lajur. Untuk memahami klausa GROUP BY dengan paparan, kami membuat paparan bernama "Info" menggunakan jadual asas "Student_info" dengan data berikut -
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)
Create or Replace View view_name AS Select_statements FROM table GROUP BY expression1, expression2, ... expression_n;
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)
Pertanyaan di atas akan mengandungi dua lajur dalam klausa GROUP BY.
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)
Set keputusan di atas menunjukkan bahawa klausa GROUP BY mengumpulkan set baris ke dalam set baris ringkasan mengikut nilai lajur.
Atas ialah kandungan terperinci Bagaimanakah kita boleh membuat paparan MySQL menggunakan klausa GROUP BY?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!