SQL基本语法(三)计算
计数
SELECT COUNT(*) FROM table_name; 总数
select count(*) from table_name where price=0; 价格等于0的数量
select price, count(*) from table_name group by price; //Count the number of apps at each price.
select price,count(*) from table_name where column > 20000 group by price;
SELECT SUM(column) FROM table_name;
SELECT category, SUM(column) FROM fake_apps GROUP BY category;
SELECT MAX(column) FROM fake_apps;
SELECT MIN(downloads) FROM fack_apps;
SELECT AVG(column) FROM fake_apps;
SELECT name,category,MAX(downloads) FROM fake_apps group by category;
select name,category,min(downloads) from fake_apps group by category;
SELECT price,AVG(downloads) FROM fake_apps GROUP BY price;
取2位小数
select price,round(avg(downloads),2) from fake_apps group by price;
总结:
Aggregate functions combine multiple rows together to form a single value of more meaningful information.
COUNT takes the name of a column(s) as an argument and counts the number of rows where the value(s) is not NULL.
GROUP BY is a clause used with aggregate functions to combine data from one or more columns.
SUM() takes the column name as an argument and returns the sum of all the values in that column.
MAX() takes the column name as an argument and returns the largest value in that column.
MIN() takes the column name as an argument and returns the smallest value in that column.
AVG() takes a column name as an argument and returns the average value for that column.
ROUND() takes two arguments, a column name and the number of decimal places to round the values in that column.