我有一个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
这是我的预订表结构
请参阅编辑/更新 1
更新 1:为什么此代码有效 http://sqlfiddle.com/#!9/acf65/2为什么它在 phpMyadmin
中给出错误
问题: phpMyadmin version
是 10.1.13-MariaDB
但是当我执行 mysql 5.6
的这个( http://sqlfiddle.com/#!9/4a543/1 )查询时为什么不起作用?
非常感谢!!!
最佳答案
我看到两个语法错误:
- 在
when
之后打开一个在CASE
语句中无效的括号。您关闭了AS
之前的括号,因此您也应该从那里删除它。 case
语句以end
结尾,而您没有这样的语句。将其放在as
之前
示例:
CASE option
WHEN condition THEN statement
[ELSE] statement
END
此外,您的第一个查询似乎是有效的 MySQL
查询。你为什么不用那个呢?
关于php - 为什么尽管将其转换为 mysql,该查询仍不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44505051/