sql group by语法与实例
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
来看一个group by实例
比如表里内容是这样
gameid best top spam
111 1 1 1
121 1 1 0
123 0 1 1
111 1 0 1
我现在要取出best top spam 的数量 结果应该是
gameid best top spam
111 2 1 2
121 1 1 0
123 0 1 1
select gameid, sum(best) as bestcnt, sum(top) as tocnt, sum(spam) as spamcnt
from table
group by gameid
在php教程 中使用方法
$sql = "select gameid,sum(best) as best,sum(top) as top,sum(spam) as spam, from tablename group by gameid order by gameid asc";
再来看个简单一点的实例
数据库教程表table
日期 胜负
2009-12-9 胜
2009-12-9 胜
2009-12-9 负
2009-12-9 负
2009-12-10 负
2009-12-10 胜
2009-12-10 负
查询结果
日期 胜 负
2009-12-9 2 2
2009-12-10 1 2
select 日期,
sum(decode(胜负,'胜',1,0)),
sum(decode(胜负,'负',1,0))
from 数据库表
group by 日期
韩剧再婚皇后阿里云盘全集高清1080P资源链接-韩剧再婚皇后免费阿里云盘资源下载4K高清无删减版链接
poipiku网站如何打开-poipiku官网网页版访问入口
明日方舟:终末地-云游戏畅玩入口-明日方舟:终末地-网页版在线直连地址
poki在线小游戏网页版-poki免费游戏大全入口
poki游戏入口-点开即玩-poki游戏-在线免费畅玩
原神akt大佬资源viciNeko入口链接-原神akt大佬资源网页vicineko入口链接分享