4

我需要从数据库中获取最近六个月的值。 这是我的小提琴。我需要动态获取值。现在二月。所以我需要数据库中的八月到一月的值。如果是 3 月,则值应自动更改为 10 月到 2 月。所以我需要得到最后六个月的价值。

我的代码如下。

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)

希望我的问题很清楚。提前致谢!!!

4

5 回答 5

17

对于 MYSQL:您可以使用 date_add

 SELECT * FROM ratepersqft 
WHERE date < Now() and date > DATE_ADD(Now(), INTERVAL- 6 MONTH);

对于 SQL Server:, dateadd:

  SELECT * FROM ratepersqft 
 WHERE date < Now() and date > DATEADD(Month, -6, Now());

http://www.sqlfiddle.com/#!2/1f8029/48

**请用反引号括起您的日期列名称,因为它是保留的关键字。**

MYSQL 更新:

SQLFIDDLE 演示

SELECT *
FROM ratepersqft 
WHERE date_format(date,'%Y-%m') < 
                     date_format(now(),'%Y-%m')
and date_format(date,'%Y-%m') >= 
                     date_format(now() - interval 6 month,'%Y-%m')
order by date desc;
于 2013-02-01T09:47:43.620 回答
4

这将考虑将所有日期四舍五入到月初进行比较,并且将从今天的日期开始最近 6 个月:

SELECT *
FROM ratepersqft
WHERE date >= DATE_SUB(DATE_FORMAT(CURDATE(), '%Y-%m-01'), INTERVAL 6 MONTH)
  AND date <  DATE_FORMAT(CURDATE(), '%Y-%m-01')

强制性SQLFiddle

于 2013-02-01T09:48:12.233 回答
1

SQLFiddle

如果你只想要整月

SELECT *
FROM ratepersqft
WHERE date between date_format(DATE_SUB(CURDATE(), INTERVAL 6 MONTH), '%Y-%m-01')
           and  date_sub(date_format(CURDATE(), '%Y-%m-01'), interval 1 day);
于 2013-02-01T09:48:45.727 回答
0

当您的数据为 MM-YYYY 格式并且您想要在六个月之间获取数据时,此代码非常有用。享受。

SELECT * FROM demo WHERE attendance_year_month < SUBSTRING(CURRENT_TIMESTAMP,1,11) and attendance_year_month
>=DATE_SUB(SUBSTRING(CURRENT_TIMESTAMP,1,11), INTERVAL 6 MONTH)

输出: 这里我正在处理我六个月前的数据

于 2021-09-04T07:03:55.637 回答
-1
 SELECT
YEAR(DATE) AS YEAR,
SUM(MONTH(DATE) = 1) AS JAN ,
SUM(MONTH(DATE) = 2) AS FEB ,
SUM(MONTH(DATE) = 3) AS MAR ,
SUM(MONTH(DATE) = 4) AS APR,
SUM(MONTH(DATE) = 5) AS MAY ,
SUM(MONTH(DATE) = 6) AS JUNE ,
SUM(MONTH(DATE) = 7) AS JULY ,
SUM(MONTH(DATE) = 8) AS AUG,
SUM(MONTH(DATE) = 9) AS SEP ,
SUM(MONTH(DATE) = 10) AS OCTOBER ,
SUM(MONTH(DATE) = 11) AS NOV ,
SUM(MONTH(DATE) = 12) AS DECE
FROM ratepersqft
WHERE  DATE >= NOW() - INTERVAL 6 MONTH 
GROUP BY 1;
于 2015-01-28T08:34:00.473 回答