SQL Date_Sub语法错误

时间:2014-03-25 09:29:15

标签: mysql sql syntax

我一直在看这个陈述多年,简直找不到错误你们可以帮忙吗?

        SELECT XD.*, UhED.row_class,
            (SELECT id
                FROM Comment C
                WHERE C.Excel_Data_Excel_Lists_id = XD.Excel_Lists_id
                    AND C.Excel_Data_row = XD.row
                LIMIT 1
            ) AS has_activity
        FROM User_has_Excel_Lists UhXL
        JOIN Excel_Lists XL
            ON XL.id = UhXL.Excel_Lists_id
        JOIN Excel_Data XD
            ON XD.Excel_Lists_id = XL.id
        LEFT JOIN User_has_Excel_Data UhED
            ON UhED.Excel_Data_Excel_Lists_id = XL.id
                AND UhED.Excel_Data_row = XD.row
                AND UhED.User_id = 1
        WHERE UhXL.User_id = 1
            AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2)<-- it says that the error is here
        GROUP BY XD.telephone 
        ORDER BY last_name ASC, first_name ASC

4 个答案:

答案 0 :(得分:1)

你忘了指定区间单位吗?可能是INTERVAL 2 DAYINTERVAL 2 HOUR之类的东西?

答案 1 :(得分:1)

 AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2 DAY)

试试这可能会对你有所帮助

答案 2 :(得分:0)

您可能需要指定间隔的度量(例如,天,月等)

您可以查看syntax for DATE_SUB here

答案 3 :(得分:0)

如果我理解您的问题,请按照以下说明操作: 你应该在下一行中提到日,月,小时或年

您的代码:

 AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2) 

更改代码:DAY

AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2 DAY) 

如果您在表格中使用DATE(tablename),则会收到如下错误:

错误代码(示例)(http://sqlfiddle.com/#!2/5b7e2/7):check代码编号:5

SELECT id,DATE_SUB(DAT(NOW()), INTERVAL 2 DAY) 
FROM supportContacts;  

正确代码:

SELECT id,DATE_SUB(DATE(NOW()), INTERVAL 2 DAY) 
FROM supportContacts;

请参阅此链接:http://sqlfiddle.com/#!2/5b7e2/7