Home >Database >Mysql Tutorial >SQL aggregate functions and sorting

SQL aggregate functions and sorting

php中世界最好的语言
php中世界最好的语言Original
2018-03-26 09:40:042314browse

This time I will bring you the aggregationfunctionsand sorting of SQL. What are the precautions for using SQL aggregation functions and sorting? The following is a practical case. , let’s take a look.

count Function

Syntax:

Select count(*)|count(列名) from table_name [where where_defination];Select count(id) from stu;Select count(name) from stu where math > 90;select count (name) from stu where chines+math+English> 250;

sum function

Select sum (列名) from stu [where where_defination];
Select sum (列名),sum(列名), sum(列名) from stu [where where_definition];
Select sum (列名)+sum(列名)+ sum(列名) from stu [where where_definition];
Select sum (列名)+sum(列名)+sum(列名) as 总分 from stu [where where_definition];
Select sum (列名)/count(name) from stu [where where_defination];
Select avg(math) from stu;
Select avg(math + chines + english) from stu;
Select max|min(列名) from stu;
Select max(math + chines + english) from stu;
Select min(math + chines + english) from stu;

Sort queryresults

Use order by clause to sort the query results

Select column1, column2, column3 from table order by column asc|desc

order by refers to the column to be sorted, or it can be the column name specified after select

order by The clause should be located at the end of the select statement

Select name, math from stu order by math;
select name, math+english+chines from stu order by math+english+chines desc;
select name, math+english+chines from stu where name like 'zhang%' order by math+english+chines desc;

SQL aggregate functions and sorting

SQL aggregate functions and sorting

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to php Other related articles on the Chinese website!

Recommended reading:

Detailed explanation of the use of paging query

Detailed explanation of the use of date-related functions in JS

The above is the detailed content of SQL aggregate functions and sorting. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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