DATE_ADD添加月和日间隔

时间:2017-06-19 23:21:09

标签: php mysql

因此PHP不接受以下行为:

DECLARE @T TABLE (ID INT IDENTITY(1, 1), Document_ID INT, Phase_ID INT, [User_ID] INT);

INSERT @T (Document_ID, Phase_ID, [User_ID]) VALUES
(1, 1, 1), (1, 2, 1), (1, 3, 1), (2, 3, 2), (2, 2, 2), (3, 1, 1), (3, 2, 1), (3, 3, 2);

SELECT ID, Document_ID, Phase_ID, [User_ID]
FROM
(
    SELECT *, RN = ROW_NUMBER() OVER (PARTITION BY Document_ID, [User_ID], CASE WHEN Phase_ID IN (2, 3) THEN 2 ELSE Phase_ID END ORDER BY Phase_ID DESC)
    FROM @T
) AS T
WHERE RN = 1;

但如果我这样做:

$sql = "SELECT DATE_ADD('" . $this->db->escape($data['regpro_buy_date']) . "', INTERVAL " . $year_flag . " YEAR + INTERVAL " . $mon_flag . " MONTH) AS warranty_date; "; 
在MYSql中

它工作正常。我不确定如何将它带到月和年合作的地方。

目前月份和年份定义为:

SELECT NOW() + INTERVAL 2 YEAR + INTERVAL 2 MONTH

1 个答案:

答案 0 :(得分:0)

呵呵,没关系,我能搞清楚。

$sql = "SELECT DATE_ADD(DATE_ADD('" . $this->db->escape($data['regpro_buy_date']) . "', INTERVAL " . $year_flag . " YEAR), INTERVAL " . $mon_flag . " MONTH) AS warranty_date; ";