首页 > 代码库 > GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组
GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组
1 drop table orders; 2 create table orders ( 3 o_id int auto_increment primary key, 4 orderdate date, 5 orderprice int, 6 customer varchar(100) 7 ); 8 9 insert into orders(orderdate, orderprice, customer) values(‘2008-12-29‘, 1000, ‘Bush‘);10 insert into orders(orderdate, orderprice, customer) values(‘2008-11-23‘, 1600, ‘Carter‘);11 insert into orders(orderdate, orderprice, customer) values(‘2008-10-05‘, 700, ‘Bush‘);12 insert into orders(orderdate, orderprice, customer) values(‘2008-09-28‘, 300, ‘Bush‘);13 insert into orders(orderdate, orderprice, customer) values(‘2008-08-06‘, 2000, ‘Adams‘);14 insert into orders(orderdate, orderprice, customer) values(‘2008-07-21‘, 100, ‘Carter‘);15 16 select * from orders;17 18 SELECT customer,SUM(orderprice) FROM orders19 GROUP BY customer;
我们拥有下面这个 "Orders" 表:
O_Id | OrderDate | OrderPrice | Customer |
---|---|---|---|
1 | 2008/12/29 | 1000 | Bush |
2 | 2008/11/23 | 1600 | Carter |
3 | 2008/10/05 | 700 | Bush |
4 | 2008/09/28 | 300 | Bush |
5 | 2008/08/06 | 2000 | Adams |
6 | 2008/07/21 | 100 | Carter |
我们使用下列 SQL 语句:
SELECT Customer,SUM(OrderPrice) FROM OrdersGROUP BY Customer
结果集类似这样:
Customer | SUM(OrderPrice) |
---|---|
Bush | 2000 |
Carter | 1700 |
Adams | 2000 |
GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。