Php 为什么即使将此查询转换为mysql,它仍然无法工作

Php 为什么即使将此查询转换为mysql,它仍然无法工作,php,mysql,sql-server,database,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`, CASE WHEN(`bt`.`end` >= NOW() THEN DA

我有一个
mssql
查询,我想把它转换成
mySql
,转换后查询仍然不起作用

这是我的
mssql
查询(原件)

这是我转换的查询(在这里转换:):

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`
但此Converted查询产生以下错误

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:为什么此代码工作为什么在
phpMyadmin中出现错误

问题:phpMyadmin
version
10.1.13-MariaDB
但是当我执行
mysql 5.6
的这个()查询时,它将不起作用为什么


非常感谢

我看到两个语法错误:

  • 当时,在
    之后打开一个括号,该括号在
    CASE
    语句中无效。将
    前面的括号关闭为
    ,因此也应将其删除
  • case
    语句以
    end
    结尾,您没有这样的语句。把它放在
    前面,作为
  • 例如:

    CASE option 
       WHEN condition THEN statement 
       [ELSE] statement 
    END
    

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

    请在错误日志中查看两个查询,而不是Case use IF条件。如果您有“tn”,则表示您运行了错误的查询,请检查此项

    SELECT product_id, NOW() AS current_date,
        bt.date_from AS starts_on,
        bt.date_end AS ends_on,
        IF(tn.end >= NOW(),DATEDIFF(bt.date_end, NOW()),0) AS days_remaining
        FROM bookings AS bt;
    

    为什么尽管将此查询转换为mysql,但它仍然无法工作,因为其中有27个错误。首先解决第一个问题,然后再继续。您必须将关键字“end”放在案例的末尾,而不是一个右括号什么是
    tn
    end
    为什么要将
    IF
    更改为
    案例
    ?请@CynicalSection,你能转换上面的代码吗,因为我对
    mysql
    SELECT product_id, NOW() AS current_date,
        bt.date_from AS starts_on,
        bt.date_end AS ends_on,
        IF(tn.end >= NOW(),DATEDIFF(bt.date_end, NOW()),0) AS days_remaining
        FROM bookings AS bt;