php - 为什么尽管将其转换为 mysql,该查询仍不起作用

标签 php mysql sql-server database

我有一个mssql我想将其转换为 mySql 的查询,转换后查询仍然不起作用。

这是我的 mssql查询(原始)

SELECT 
    product_id, 
    NOW() AS `current_date`,
    `bt`.`date_from` AS `starts_on`,
    `bt`.`date_end` AS `ends_on`,
    IF(`bt`.`end` >= NOW(),
         DATEDIFF(`bt`.`date_end`, NOW()), #show days until event ends
         0 #the event has already passed
    ) AS `days_remaining`

FROM `bookings` AS `bt`

这是我转换后的查询(此处转换为:http://www.sqlines.com/online):

SELECT 
    product_id, 
    NOW() AS `current_date`,
    `bt`.`date_from` AS `starts_on`,
    `bt`.`date_end` AS `ends_on`,
    CASE WHEN(`bt`.`end` >= NOW() THEN 
         DATEDIFF(`bt`.`date_end`, NOW()) ELSE  #show days until event ends
         0 #the event has already passed
    ) AS `days_remaining`

FROM `bookings` AS `bt`

但是这个转换后的查询出现以下错误

Static analysis:

27 errors were found during analysis.

An expression was expected. (near "CASE" at position 154)
Unrecognized keyword. (near "CASE" at position 154)
Unrecognized keyword. (near "WHEN" at position 159)
Unexpected token. (near "(" at position 163)
Unexpected token. (near "`tn`" at position 164)
Unexpected token. (near "." at position 168)
Unexpected token. (near "`end`" at position 169)
Unexpected token. (near ">=" at position 175)
Unrecognized keyword. (near "NOW" at position 178)
Unexpected token. (near "(" at position 181)
Unexpected token. (near ")" at position 182)
Unrecognized keyword. (near "THEN" at position 184)
Unrecognized keyword. (near "DATEDIFF" at position 204)
Unexpected token. (near "(" at position 212)
Unexpected token. (near "`bt`" at position 213)
Unexpected token. (near "." at position 217)
Unexpected token. (near "`date_end`" at position 218)
Unexpected token. (near "," at position 228)
Unrecognized keyword. (near "NOW" at position 230)
Unexpected token. (near "(" at position 233)
Unexpected token. (near ")" at position 234)
Unexpected token. (near ")" at position 235)
Unrecognized keyword. (near "ELSE" at position 237)
Unexpected token. (near "0" at position 256)
Unexpected token. (near ")" at position 266)
Unrecognized keyword. (near "AS" at position 268)
Unexpected token. (near "`days_remaining`" at position 271)
SQL query: Documentation

SELECT product_id, NOW() AS `current_date`, `bt`.`date_from` AS `starts_on`, `bt`.`date_end` AS `ends_on`, CASE WHEN(`tn`.`end` >= NOW() THEN DATEDIFF(`bt`.`date_end`, NOW()) ELSE 0 ) AS `days_remaining` FROM `bookings` AS `bt` LIMIT 0, 25

MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'THEN 
             DATEDIFF(`bt`.`date_end`, NOW()) ELSE               0       ' at line 6

这是我的预订表结构

enter image description here

请参阅编辑/更新 1

更新 1:为什么此代码有效 http://sqlfiddle.com/#!9/acf65/2为什么它在 phpMyadmin 中给出错误

问题: phpMyadmin version10.1.13-MariaDB但是当我执行 mysql 5.6 的这个( http://sqlfiddle.com/#!9/4a543/1 )查询时为什么不起作用?

非常感谢!!!

最佳答案

我看到两个语法错误:

  1. when 之后打开一个在 CASE 语句中无效的括号。您关闭了 AS 之前的括号,因此您也应该从那里删除它。
  2. case 语句以 end 结尾,而您没有这样的语句。将其放在as
  3. 之前

示例:

CASE option 
   WHEN condition THEN statement 
   [ELSE] statement 
END

此外,您的第一个查询似乎是有效的 MySQL 查询。你为什么不用那个呢?

关于php - 为什么尽管将其转换为 mysql,该查询仍不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44505051/

相关文章:

php - 此查询的可能 JOIN

php - 存储时间为 00 :00:00 的时间戳

sql - 具有不同时间戳的重复条目

sql-server - 为什么 equal 运算符和 like 运算符之间的相等比较存在差异?

php - 只选择一列不同的mysql

php - 您允许文档管理应用程序使用哪些文件类型/扩展名?

php - 正则表达式在第一个换行符处拆分字符串

php - Laravel 中的错误爆炸

mysql - SQL:在这种情况下如何按创建的sql顺序查询

python - SQLCLR 和 IronPython