欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SQL GROUP BY 语句

程序员文章站 2022-05-08 14:33:59
...

    GROUP BY 语句可结合一些聚合函数来使用。

    GROUP BY 语句用于结合聚合函数,根据一个或多个列对结果集进行分组。


SQL GROUP BY 语法

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

演示数据库

SQL GROUP BY 语句


GROUP BY 简单应用

SELECT site_id, SUM(access_log.count) AS nums
FROM access_log GROUP BY site_id;  //统计各个site_id的访问量
SQL GROUP BY 语句  

SQL GROUP BY 多表连接

SELECT Websites.name,COUNT(access_log.aid) AS nums FROM access_log
LEFT JOIN Websites
ON access_log.site_id=Websites.id
GROUP BY Websites.name;
SQL GROUP BY 语句

相关标签: SQL GROUP BY