表记录为如下:
产品名 月数范围 价格
200M以上 12->36月 350元/M 月
200M以上 3->11月 410元/M 月
200M以上 1->2月 450元/M 月 需要统计成如下:
产品名 月付 季付 年付
1-9M 600元/M 月 560元/M 月 500元/M 月
10-49M 550元/M 月 510元/M 月 450元/M 月
50-199M 500元/M 月 460元/M 月 400元/M 月
200M以上 450元/M 月 410元/M 月 350元/M 月请问SQL如何写?
产品名 月数范围 价格
200M以上 12->36月 350元/M 月
200M以上 3->11月 410元/M 月
200M以上 1->2月 450元/M 月 需要统计成如下:
产品名 月付 季付 年付
1-9M 600元/M 月 560元/M 月 500元/M 月
10-49M 550元/M 月 510元/M 月 450元/M 月
50-199M 500元/M 月 460元/M 月 400元/M 月
200M以上 450元/M 月 410元/M 月 350元/M 月请问SQL如何写?
pname产品名,rangemin最小月数 rangemax最大月数price价格
price表结构
`priceid` bigint(20) NOT NULL AUTO_INCREMENT,
`productid` bigint(20) DEFAULT NULL, 产品id
`addtime` datetime DEFAULT NULL,
`rangemin` int(11) DEFAULT NULL, 最小月数
`rangemax` int(11) DEFAULT NULL, 最大月数
`price` float DEFAULT NULL, 价格
`expiration` int(11) DEFAULT '-1',
`state` smallint(6) NOT NULL DEFAULT '1' COMMENT '1 正常,2删除',
数据就是上面列出的~
下面这个结果是怎么算出来的?逻辑是什么?
1-9M 600元/M 月 560元/M 月 500元/M 月
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
product CREATE TABLE `product` (
`productid` bigint(20) NOT NULL AUTO_INCREMENT,
`pname` varchar(100) DEFAULT NULL,
`ptype` int(11) DEFAULT NULL COMMENT '产品类型:1:带宽 0:流量',
`nodetype` int(11) DEFAULT NULL COMMENT '''第一节点'':1,''第一节点'':2',
`unit` varchar(10) DEFAULT NULL,
`comment` varchar(255) DEFAULT NULL,
`state` smallint(4) NOT NULL DEFAULT '1' COMMENT '(1正常,2删除)',
PRIMARY KEY (`productid`)
) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
insert into product(pname,ptype,nodetype,unit,comment,state) values('1-9M',1,1,'月','1-9M备注',1);price CREATE TABLE `price` (
`priceid` bigint(20) NOT NULL AUTO_INCREMENT,
`productid` bigint(20) DEFAULT NULL,
`addtime` datetime DEFAULT NULL,
`rangemin` int(11) DEFAULT NULL,
`rangemax` int(11) DEFAULT NULL,
`price` float DEFAULT NULL,
`expiration` int(11) DEFAULT '-1',
`state` smallint(6) NOT NULL DEFAULT '1' COMMENT '1 正常,2删除',
PRIMARY KEY (`priceid`),
KEY `FK_Reference_2` (`productid`)
) ENGINE=MyISAM AUTO_INCREMENT=86 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
insert into price(productid,addtime,rangemin,rangemax,price,expiration,state) values(1,now(),1,2,600,-1,1);
insert into price(productid,addtime,rangemin,rangemax,price,expiration,state) values(1,now(),3,11,560,-1,1);
insert into price(productid,addtime,rangemin,rangemax,price,expiration,state) values(1,now(),12,36,500,-1,1);
需要查出结果:
产品名 月付 季付 年付
1-9M 600元/M 月 560元/M 月 500元/M 月
...
SELECT a.ptype,a.pname,SUM(IF(b.rangemin=1,b.price,0)) AS 月付,
SUM(IF(b.rangemin=3,b.price,0)) AS 季付,
SUM(IF(b.rangemin=12,b.price,0)) AS 年付
FROM product a LEFT JOIN price b
ON a.ptype=b.productid
GROUP BY a.ptype,a.pname
SELECT a.ptype,a.pname,SUM(IF(b.rangemin>=1 AND b.rangemax<=2,b.price,0)) AS 月付,
SUM(IF(b.rangemin>=3 AND b.rangemax<=11,b.price,0)) AS 季付,
SUM(IF(b.rangemin>=12 AND b.rangemax<=36,b.price,0)) AS 年付
FROM product a LEFT JOIN price b
ON a.ptype=b.productid
GROUP BY a.ptype,a.pname