MySQL - 无法从MSSQL

时间:2016-10-21 22:43:39

标签: mysql sql-server

尝试将此MSSQL 2012R2语句转换为MySQL 5.7

SELECT SUM(Cost + Credit_Amount) As Daily_Cost FROM Google_Costing
WHERE Date BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE() -2), 0) AND     
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE() -2) + 1, 0)

我尝试将DATEADD替换为DATE_ADD,将DATEDIFF替换为TIMESTAMPDIFF,但仍然没有运气。任何帮助表示赞赏。

我收到错误:

  

调用原生函数' DATEDIFF'

时参数计数不正确

所以我将语句改为:

SELECT SUM(Cost + Credit_Amount) As Daily_Cost FROM Google_Costing
WHERE Date BETWEEN DATE_ADD(MONTH, TIMESTAMPDIFF(MONTH, 0, GETDATE() -2), 0) AND 
DATE_ADD(MONTH, TIMESTAMPDIFF(MONTH, 0, GETDATE() -2) + 1, 0)

这次我收到错误:

  

您的SQL语法有错误;检查对应的手册   您的MySQL服务器版本,以便使用正确的语法   接近' TIMESTAMPDIFF(月,0,GETDATE() - 2),0)和DATE_ADD(月,   TIMESTAMPDIFF(MONT'第2行

1 个答案:

答案 0 :(得分:0)

试试这些饼干......

SELECT SUM(Cost + Credit_Amount) As Daily_Cost FROM Google_Costing
WHERE Date BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01') 
AND DATE_ADD(CURDATE(), INTERVAL -2 DAY)