我需要從數據庫中獲取最近6個月的值。 這是我的Fiddle。我需要動態獲取這些值。現在二月。所以我需要8月到1月的數據庫值。如果是3月份,那麼這些值應該會自動更改爲10月至2月。所以我需要得到最後六個月的價值。獲取mysql中最近6個月的值
我的代碼如下。
CREATE TABLE IF NOT EXISTS `ratepersqft` (
`price_id` int(11) NOT NULL auto_increment,
`project_id` int(11) default NULL,
`date` varchar(255) default NULL,
`rate_per_sqft` double default NULL,
`common_location` varchar(255) default NULL,
PRIMARY KEY (`price_id`)
);
INSERT INTO `ratepersqft` (`price_id`, `project_id`, `date`, `rate_per_sqft`, `common_location`) VALUES
(1, 1, '2012-07-01 ', 2800, 'ECR'),
(2, 2, '2012-07-02 ', 1550, 'ECR'),
(3, 3, '2012-07-03 ', 1850, 'ECR'),
(4, 4, '2012-07-04 ', 4425, 'ECR'),
(5, 5, '2012-07-05 ', 2300, 'ECR'),
(6, 1, '2012-08-01 ', 2900, 'ECR'),
(7, 2, '2012-08-02 ', 1650, 'ECR'),
(8, 3, '2012-08-03 ', 1950, 'ECR'),
(9, 4, '2012-08-04 ', 4525, 'ECR'),
(10, 5, '2012-08-05 ', 2200, 'ECR'),
(11, 1, '2012-09-01 ', 3000, 'ECR'),
(12, 2, '2012-09-02 ', 1450, 'ECR'),
(13, 3, '2012-09-03 ', 2050, 'ECR'),
(14, 4, '2012-09-04 ', 4625, 'ECR'),
(15, 5, '2012-09-05 ', 2100, 'ECR'),
(16, 1, '2012-10-01 ', 3100, 'ECR'),
(17, 2, '2012-10-02 ', 2150, 'ECR'),
(18, 3, '2012-10-03 ', 1850, 'ECR'),
(19, 4, '2012-10-04 ', 4725, 'ECR'),
(20, 5, '2012-10-05 ', 1900, 'ECR'),
(21, 1, '2012-11-01 ', 3200, 'ECR'),
(22, 2, '2012-11-02 ', 2250, 'ECR'),
(23, 3, '2012-11-03 ', 1850, 'ECR'),
(24, 4, '2012-11-04 ', 4825, 'ECR'),
(25, 5, '2012-11-05 ', 2300, 'ECR'),
(26, 1, '2012-12-01 ', 3300, 'ECR'),
(27, 2, '2012-12-02 ', 2350, 'ECR'),
(28, 3, '2012-12-03 ', 1850, 'ECR'),
(29, 4, '2012-12-04 ', 4925, 'ECR'),
(30, 5, '2012-12-05 ', 2400, 'ECR'),
(31, 1, '2013-01-01 ', 3400, 'ECR'),
(32, 2, '2013-01-02 ', 2000, 'ECR'),
(33, 3, '2013-01-03 ', 2450, 'ECR'),
(34, 4, '2013-01-04 ', 5025, 'ECR'),
(35, 5, '2013-01-05 ', 2500, 'ECR'),
(36, 1, '2013-02-01 ', 3500, 'ECR'),
(37, 2, '2013-02-02 ', 2100, 'ECR'),
(38, 3, '2013-02-03 ', 2550, 'ECR'),
(39, 4, '2013-02-04 ', 5125, 'ECR'),
(40, 5, '2013-02-05 ', 2600, 'ECR'),
(41, 1, '2012-06-01 ', 1800, 'ECR'),
(42, 2, '2012-06-02 ', 1150, 'ECR'),
(43, 3, '2012-06-03 ', 1350, 'ECR'),
(44, 4, '2012-06-04 ', 3425, 'ECR'),
(45, 5, '2012-06-05 ', 2100, 'ECR');
我試着這個查詢,它給我所有的數據庫中的值。
SELECT *
FROM ratepersqft
WHERE MONTH(date) < DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
希望我的問題很明確。提前致謝!!!
您已經標記爲MySQL和SQL數據庫......究竟哪一個? :-) – bonCodigo
這是mysql。你可以參考小提琴 –
啊,你沒有指定將來的記錄應該排除。我正在更改我的答案以添加此要求 – mvp