用户,
购买商品,
消费时间,
消费金额
----------------------我是一条分割线--------------------------
--------------------------------------------------------------------------
如何使用一条Sql语句查出表里的所有记录并且查出总的消费金额呢
3 个解决方案
#1
大神们你们在哪,求指导
#2
是不是没有办法一条sql搞定呢
#3
CREATE TABLE `consume` (
`ID` int(6) NOT NULL auto_increment,
`user` varchar(64) DEFAULT NULL,
`product` varchar(64) DEFAULT NULL,
`time` DATETIME,
`monetary` int(6) DEFAULT -1,
PRIMARY KEY (`id`)
) ENGINE=MEMORY DEFAULT CHARSET=utf8;
INSERT INTO consume(user,product,time,monetary) values('user1','iphone',STR_TO_DATE('2018-09-05 10:17:00', '%Y-%m-%d %H:%i:%s') ,10000);
INSERT INTO consume(user,product,time,monetary) values('user2','iphone3',STR_TO_DATE('2013-09-05 10:17:00', '%Y-%m-%d %H:%i:%s') ,90000);
INSERT INTO consume(user,product,time,monetary) values('user3','iphone4',STR_TO_DATE('2014-09-05 10:17:00', '%Y-%m-%d %H:%i:%s') ,70000);
INSERT INTO consume(user,product,time,monetary) values('user4','iphone5',STR_TO_DATE('2015-09-05 10:17:00', '%Y-%m-%d %H:%i:%s') ,80000);
INSERT INTO consume(user,product,time,monetary) values('user5','iphone6',STR_TO_DATE('2016-09-05 10:17:00', '%Y-%m-%d %H:%i:%s') ,60000);
INSERT INTO consume(user,product,time,monetary) values('user6','iphone8',STR_TO_DATE('2017-09-05 10:17:00', '%Y-%m-%d %H:%i:%s') ,90000);
SELECT
a.*, b.*
FROM
consume a,
(
SELECT
sum(monetary)
FROM
consume
) b;
SELECT
a.*, b.*
FROM
consume a,
(
SELECT
sum(monetary),
product
FROM
consume
GROUP BY
product
) b
WHERE
a.product = b.product;
#1
大神们你们在哪,求指导
#2
是不是没有办法一条sql搞定呢
#3
CREATE TABLE `consume` (
`ID` int(6) NOT NULL auto_increment,
`user` varchar(64) DEFAULT NULL,
`product` varchar(64) DEFAULT NULL,
`time` DATETIME,
`monetary` int(6) DEFAULT -1,
PRIMARY KEY (`id`)
) ENGINE=MEMORY DEFAULT CHARSET=utf8;
INSERT INTO consume(user,product,time,monetary) values('user1','iphone',STR_TO_DATE('2018-09-05 10:17:00', '%Y-%m-%d %H:%i:%s') ,10000);
INSERT INTO consume(user,product,time,monetary) values('user2','iphone3',STR_TO_DATE('2013-09-05 10:17:00', '%Y-%m-%d %H:%i:%s') ,90000);
INSERT INTO consume(user,product,time,monetary) values('user3','iphone4',STR_TO_DATE('2014-09-05 10:17:00', '%Y-%m-%d %H:%i:%s') ,70000);
INSERT INTO consume(user,product,time,monetary) values('user4','iphone5',STR_TO_DATE('2015-09-05 10:17:00', '%Y-%m-%d %H:%i:%s') ,80000);
INSERT INTO consume(user,product,time,monetary) values('user5','iphone6',STR_TO_DATE('2016-09-05 10:17:00', '%Y-%m-%d %H:%i:%s') ,60000);
INSERT INTO consume(user,product,time,monetary) values('user6','iphone8',STR_TO_DATE('2017-09-05 10:17:00', '%Y-%m-%d %H:%i:%s') ,90000);
SELECT
a.*, b.*
FROM
consume a,
(
SELECT
sum(monetary)
FROM
consume
) b;
SELECT
a.*, b.*
FROM
consume a,
(
SELECT
sum(monetary),
product
FROM
consume
GROUP BY
product
) b
WHERE
a.product = b.product;