Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of group by in mysql

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you the use of group by in mysql, I hope you read this article after the harvest, let us discuss it together!

Select * FROM table_name WHERE column_name GROUP BY column_name; The group by statement groups the result set according to one or more columns.

The GROUP BY statement groups the result set according to one or more columns.

On grouped columns we can use COUNT, SUM, AVG, etc.

(Recommended tutorial: mysql video tutorial)

Syntax format:

SELECT column_name, function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_name;

Examples:

SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0;-- ------------------------------ Table structure for `employee_tbl`-- ----------------------------DROP TABLE IF EXISTS `employee_tbl`; CREATE TABLE `employee_tbl`( `id` int(11) NOT NULL, `name` char(10) NOT NULL DEFAULT '', `date` datetime NOT NULL, `singin` tinyint(4) NOT NULL DEFAULT '0' COMMENT 'entry number', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ------------------------------ Records of `employee_tbl` -- ---------------------------- BEGIN;INSERT INTO `employee_tbl` VALUES ('1',' Xiaoming ', '2016-04-22 15:25: 33','1'), ('2',' Xiao Wang','2016-04-20 15:25: 47',' 3'), ('3',' Xiaoli ', '2016-04-19 15:26: 02','2'), ('4',' Xiao Wang','2016-04-07 15:26: 14',' 4'), ('5',' Xiaoming ', '2016-04-11 15:26: 40','4'), ('6',' Xiaoming ', '2016-04-04 15:26: 54','2'); COMMIT;SET FOREIGN_KEY_CHECKS = 1;

After successful import, execute the following SQL statement:

Next, we use the GROUP BY statement to group the data table by name and count how many records each person has:

After reading this article, I believe you have a certain understanding of the usage of group by in mysql. If you want to know more about it, welcome to pay attention to the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report