mysql sum join 多表统计求和时不正确的问题
2015-03-18 11:44
387 查看
我在做订单统计的时候遇到问题,就是多表求和时发现不正确
我有下面两个表
平时的统计是这样用的, userid 是表示当前用户
SELECT oid , SUM( priceCount ) AS count_price
FROM mobile_shops_orders AS s
WHERE s.userid = '2'
这里的值372是我们想要的
多表查询时统计时是下面这样的
但是我们发现,这个数据是错误的
于是我想到了用 distinct 但是发现用不了
于是又想到 group by
出来的结果是下面这样的
发现统计只是分组统计,没达到所有行统计,而且统计的数据不正确
于是我写了下面的语句来查看结果,加多了一个count函数查看
出来的结果是下面的,一看就知道是什么原因了
最后我们的语句应该是下面这个的,join 后面这个是重点,意思是返回不重复的数据
这里有几个要注意的问题,一是,group by 是用于分组统计,如果要所有行统计不能用他
二需要使用子句先排除重复行,否则也会出得统计不正确的问题
还有,看网上说 返回 null 时sum等统计函数是不正确的
不过我们建表是都应该设置为not null 这样就不用担心这个问题了,而且还能加快访问
我有下面两个表
-- -- 表的结构 `mobile_shops_orders` -- CREATE TABLE IF NOT EXISTS `mobile_shops_orders` ( `oid` varchar(80) NOT NULL, `userid` int(10) NOT NULL, `pid` int(10) NOT NULL DEFAULT '0', `paytype` tinyint(2) NOT NULL DEFAULT '1', `cartcount` int(10) NOT NULL DEFAULT '0', `dprice` float(13,2) NOT NULL DEFAULT '0.00', `price` float(13,2) NOT NULL DEFAULT '0.00', `priceCount` float(13,2) NOT NULL, `state` tinyint(1) NOT NULL DEFAULT '0', `ip` char(15) NOT NULL DEFAULT '', `stime` int(10) NOT NULL DEFAULT '0', `rebateCount` float NOT NULL COMMENT '累计优惠价格', KEY `stime` (`stime`), KEY `userid` (`userid`), KEY `oid` (`oid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; -- -- 转存表中的数据 `mobile_shops_orders` -- INSERT INTO `mobile_shops_orders` (`oid`, `userid`, `pid`, `paytype`, `cartcount`, `dprice`, `price`, `priceCount`, `state`, `ip`, `stime`, `rebateCount`) VALUES ('2015031710015621508', 1, 1, 1, 8, 10.00, 728.00, 728.00, 0, '127.0.0.1', 1426561489, 10), ('2015031711044980920', 1, 1, 1, 19, 10.00, 855.50, 865.50, 0, '127.0.0.1', 1426581574, 0), ('2015031716393491386', 1, 1, 1, 8, 10.00, 600.00, 610.00, 0, '127.0.0.1', 1426588329, 0), ('2015031809153611521', 2, 1, 1, 6, 10.00, 322.00, 332.00, 0, '127.0.0.1', 1426641353, 0), ('2015031809155386138', 2, 1, 1, 3, 10.00, 30.00, 40.00, 0, '127.0.0.1', 1426641377, 0);
-- -- 数据库: `mobile` -- -- -------------------------------------------------------- -- -- 表的结构 `mobile_shops_products` -- CREATE TABLE IF NOT EXISTS `mobile_shops_products` ( `aid` mediumint(8) NOT NULL DEFAULT '0', `oid` varchar(80) NOT NULL DEFAULT '', `userid` int(10) NOT NULL, `title` varchar(80) NOT NULL DEFAULT '', `price` float(13,2) NOT NULL DEFAULT '0.00', `buynum` int(10) NOT NULL DEFAULT '9', KEY `oid` (`oid`), KEY `userid` (`userid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; -- -- 转存表中的数据 `mobile_shops_products` -- INSERT INTO `mobile_shops_products` (`aid`, `oid`, `userid`, `title`, `price`, `buynum`) VALUES (8, '2015031710015621508', 1, '盛盈汇贵妃鸡 光鸡约3.5斤', 139.00, 5), (9, '2015031710015621508', 1, '紫玉淮山 5斤/份', 11.00, 3), (14, '2015031711044980920', 1, '紫玉淮山 5斤/份aaaaaaaaa', 12.50, 7), (8, '2015031711044980920', 1, '盛盈汇贵妃鸡 光鸡约3.5斤', 139.00, 5), (9, '2015031711044980920', 1, '紫玉淮山 5斤/份', 11.00, 3), (10, '2015031711044980920', 1, '盛盈汇贵妃鸡 光鸡约', 10.00, 4), (8, '2015031716393491386', 1, '盛盈汇贵妃鸡 光鸡约3.5斤', 139.00, 4), (9, '2015031716393491386', 1, '紫玉淮山 5斤/份', 11.00, 4), (8, '2015031809153611521', 2, '盛盈汇贵妃鸡 光鸡约3.5斤', 139.00, 2), (9, '2015031809153611521', 2, '紫玉淮山 5斤/份', 11.00, 4), (10, '2015031809155386138', 2, '盛盈汇贵妃鸡 光鸡约', 10.00, 3);
平时的统计是这样用的, userid 是表示当前用户
SELECT oid , SUM( priceCount ) AS count_price
FROM mobile_shops_orders AS s
WHERE s.userid = '2'
oid | count_price |
---|---|
2015031809153611521 | 372.00 |
多表查询时统计时是下面这样的
SELECT s.oid,p.oid AS poid, SUM(priceCount) AS count_price FROM mobile_shops_orders AS s LEFT JOIN mobile_shops_products AS p ON p.oid = s.oid WHERE s.userid = '2'
oid | poid | count_price |
---|---|---|
2015031809153611521 | 2015031809153611521 | 704.00 |
于是我想到了用 distinct 但是发现用不了
SELECT s.oid,p.oid AS poid, SUM( priceCount) AS count_price,DISTINCT poid FROM mobile_shops_orders AS s LEFT JOIN mobile_shops_products AS p ON p.oid = s.oid WHERE s.userid = '2'
<span style="font-family: Arial, Helvetica, sans-serif;">--这个语句是错的</span>
于是又想到 group by
SELECT s.oid,p.oid AS poid, SUM( priceCount) AS count_price FROM mobile_shops_orders AS s LEFT JOIN mobile_shops_products AS p ON p.oid = s.oid WHERE s.userid = '2' GROUP BY poid
出来的结果是下面这样的
oid | poid | count_price |
---|---|---|
2015031809153611521 | 2015031809153611521 | 664.00 |
2015031809155386138 | 2015031809155386138 | 40.00 |
于是我写了下面的语句来查看结果,加多了一个count函数查看
SELECT s.oid,p.oid AS poid, SUM( priceCount) AS count_price, COUNT(p.oid) FROM mobile_shops_orders AS s LEFT JOIN mobile_shops_products AS p ON p.oid = s.oid WHERE s.userid = '2' GROUP BY poid
出来的结果是下面的,一看就知道是什么原因了
oid | poid | count_price | COUNT(p.oid) |
---|---|---|---|
2015031809153611521 | 2015031809153611521 | 664.00 | 2 |
2015031809155386138 | 2015031809155386138 | 40.00 | 1 |
SELECT s.oid, p.oid AS poid, SUM(priceCount) AS count_price
FROM mobile_shops_orders AS s
<span style="font-family: Arial, Helvetica, sans-serif;">LEFT JOIN (select distinct oid from mobile_shops_products) AS p </span>
<span style="font-family: Arial, Helvetica, sans-serif;">ON p.oid = s.oid </span>
<span style="font-family: Arial, Helvetica, sans-serif;">WHERE s.userid = '2'</span>
<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
oid | poid | count_price |
---|---|---|
2015031809153611521 | 2015031809153611521 | 372.00 |
二需要使用子句先排除重复行,否则也会出得统计不正确的问题
还有,看网上说 返回 null 时sum等统计函数是不正确的
oid | poid | count_price | COUNT(p.oid) |
---|---|---|---|
2015031809153611521 | 2015031809153611521 | 664.00 | 2 |
2015031809155386138 | 2015031809155386138 | 40.00 | 1 |
oid | poid | count_price |
---|---|---|
2015031809153611521 | 2015031809153611521 | 664.00 |
2015031809155386138 | 2015031809155386138 | 40.00 |
相关文章推荐
- MYSQL中有关SUM字段按条件统计使用IF函数(case)问题
- mysql语句sum求和为null的问题
- MYSQL中有关SUM字段按条件统计使用IF函数(case)问题
- MYSQL中有关SUM字段按条件统计使用IF函数(case)问题
- MySQL数据库中如何解决分组统计的问题 - MySQL
- MySql 的统计查询性能问题
- 连接mysql server 5.0 数据库的乱码问题,utf8编码,中文正确显示
- 解决MySQL“由于连接方在一段时间后没有正确答复或连接的主机没有反应”的问题
- 正确使用mysql + MFC的一个要注意问题
- MYSQL中SUM字段按条件统计使用IF函数(case)
- 怎样正确的解决MySQL中文模糊检索问题
- MySql 字段类型为float时,SUM汇总时出现一堆小数的问题
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
- MySQL 中文模糊查询不正确问题解决方案
- 关于MySql 4.0.12左联接left outer join的一些问题
- Mysql 解决 分组统计问题
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
- 编码素养问题收集4:MySQL数据导入导出如何证明结果是正确的?