Mysql_06_ using aggregate functions to query

Source: Internet
Author: User

#使用数据库
Use db_student;

#创建表
CREATE TABLE ' T_grade ' (
' id ' int PRIMARY KEY auto_increment not NULL,
' Stuname ' varchar (60),
' Course ' varchar (60),
' Score ' int
);

#插入数据
INSERT INTO ' t_grade ' (' id ', ' stuname ', ' Course ', ' score ') VALUES (' 1 ', ' Zhang San ', ' language ', ' the ') ';
Insert INTO ' t_grade ' (' id ', ' stuname ', ' Course ', ' score ') VALUES (' 2 ', ' Zhang San ', ' math ', ' 90 ');
INSERT INTO ' t_grade ' (' id ', ' stuname ', ' Course ', ' score ') VALUES (' 3 ', ' Zhang San ', ' English ', ' 87 ');
INSERT INTO ' t_grade ' (' id ', ' stuname ', ' Course ', ' score ') VALUES (' 4 ', ' John Doe ', ' language ', ' 79 ');
INSERT INTO ' t_grade ' (' id ', ' stuname ', ' Course ', ' score ') VALUES (' 5 ', ' John Doe ', ' math ', ' 95 ');
INSERT INTO ' t_grade ' (' id ', ' stuname ', ' Course ', ' score ') VALUES (' 6 ', ' John Doe ', ' English ', ' 80 ');
INSERT INTO ' t_grade ' (' id ', ' stuname ', ' Course ', ' score ') VALUES (' 7 ', ' Harry ', ' language ', ' 77 ');
INSERT INTO ' t_grade ' (' id ', ' stuname ', ' Course ', ' score ') VALUES (' 8 ', ' Harry ', ' Math ', ' 81 ');
INSERT INTO ' t_grade ' (' id ', ' stuname ', ' Course ', ' score ') VALUES (' 9 ', ' Harry ', ' English ', ' "");


#第五章: Single-table operation
#第二节: Querying with aggregate functions
#5.1:count () function
SELECT COUNT (*) from T_grade; #统计记录的条数
SELECT COUNT (*) as total from T_grade; #添加别名
Select Stuname,count (Course) as "number of accounts" from T_grade GROUP by Stuname,course;
Select Stuname,group_concat (Course) as "account" from the T_grade GROUP by Stuname;

#5.2:sum () function
Select Stuname,group_concat (Course) as "account", SUM (score) as "total Score" from T_grade WHERE stuname= "Zhang San";
Select Stuname,group_concat (Course) as "account", SUM (score) as "total Score" from the T_grade GROUP by Stuname;

#5.3:avg () function
Select Stuname,group_concat (Course) as "account", AVG (score) as "average score" from T_grade WHERE stuname= "John Doe";
Select Stuname,group_concat (Course) as "account", AVG (score) as "average score" from T_grade GROUP by Stuname;

#5.4:max () function
Select Stuname,max (Score) as "fraction" from T_grade WHERE stuname= "Harry";
Select Stuname,max (Score) as "highest score" from T_grade GROUP by Stuname;

#5.5:min () function
Select Stuname,min (Score) as "lowest score" from T_grade WHERE stuname= "John Doe";
Select Stuname,min (Score) as "minimum score" from T_grade GROUP by Stuname;

Mysql_06_ using aggregate functions to query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.