我有两张桌子
--
-- Table structure for table `d_statistics_report_temp`
--
CREATE TABLE IF NOT EXISTS `d_statistics_report_temp` (
`srt_id` int(10) NOT NULL AUTO_INCREMENT ,
`pub_name` varchar(100) DEFAULT NULL ,
`sal_must_pay_date` date DEFAULT NULL ,
`total_sales` float(7,0) DEFAULT NULL,
`total_must_pay` float(7,0) NOT NULL,
`pub_share` float(7,0) DEFAULT NULL ,
`pub_id` int(11) DEFAULT NULL COMMENT ,
`conversion_doc` varchar(255) NOT NULL,
PRIMARY KEY (`srt_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
--
-- Dumping data for table `d_statistics_report_temp`
--
INSERT INTO `d_statistics_report_temp`
(`srt_id`, `pub_name`, `sal_must_pay_date`, `total_sales`, `total_must_pay`, `pub_share`, `pub_id`, `conversion_doc`)
VALUES
(1, 'sara', '2013-03-01', 50, 0, 15, 256, ''),
(2, 'sara', '2013-04-01', 20, 0, 15, 256, ''),
(3, 'sara', '2013-05-01', 80, 0, 15, 256, ''),
(4, 'sara', '2013-06-01', 10, 0, 15, 256, ''),
(5, 'sara', '2013-07-01', 20, 0, 15, 256, ''),
(6, 'sara', '2013-08-01', 70, 0, 15, 256, ''),
(7, 'sara', '2013-09-01', 90, 0, 15, 256, ''),
(8, 'adam', '2013-05-01', 10, 0, 15, 255, '')
和第二张桌子
--
-- Table structure for table `d_statistics_docs`
--
CREATE TABLE IF NOT EXISTS `d_statistics_docs` (
`sd_id` int(11) NOT NULL AUTO_INCREMENT,
`sd_pub` int(11) NOT NULL,
`sd_date` date NOT NULL,
`sd_doc` text NOT NULL,
`price` int(11) NOT NULL,
PRIMARY KEY (`sd_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;
--
-- Dumping data for table `d_statistics_docs`
--
INSERT INTO `d_statistics_docs` (`sd_id`, `sd_pub`, `sd_date`, `sd_doc`, `price`) VALUES
(9, 256, '2013-03-01', 'scifiwallpaper1.jpg'),
(10, 256, '2013-04-01', 'scifiwallpaper1.jpg'),
(11, 256, '2013-05-01', ''),
(12, 256, '2013-06-01', 'scifiwallpaper1.jpg'),
(13, 256, '2013-07-01', ''),
(14, 256, '2013-08-01', ''),
(15, 256, '2013-09-01', 'scifiwallpaper1.jpg');
我想加入表格以获取total_sales
每个pub_id
(publisher_id)的每个sd_date
但我只想为此when sd_doc IS NULL
设置total_sales
条件sd_date
如果sd_doc NOT NULL
返回total_sales
本月和之前的所有月份
输出示例
9 256 2013-03-01 scifiwallpaper1.jpg 50
10 256 2013-04-01 scifiwallpaper1.jpg 20
11 256 2013-05-01 NULL 80
12 256 2013-06-01 scifiwallpaper1.jpg 90 // total 10 + 80
13 256 2013-07-01 NULL 20
14 256 2013-08-01 NULL 70
15 256 2013-09-01 scifiwallpaper1.jpg 180 // total of 90+70+20
*注意:第二张表中的 sal_must_pay_date = sd_date*