私は2つのテーブルを持っています
--
-- 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, '')
そして2つ目のテーブル
--
-- 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
for every pub_id
(publisher_id) for everyを取得したいsd_date
しかし、これだけに条件when sd_doc IS NULL
getを入れたい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 = 2 番目のテーブルの sd_date*