2017-06-12 29 views
0

我有一个mssql查询,我想将它转换成mySql,转换后仍然查询不起作用。为什么这个查询不工作,尽管它转换为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但当我执行此(http://sqlfiddle.com/#!9/4a543/1)查询mysql 5.6它不会工作,为什么?

非常感谢你!

+2

_why此查询不工作,尽管其转换成mysql_因为在这27个错误。 – RiggsFolly

+0

您必须将关键字“end”放在您的案例的末尾,而不是右括号 – Osuwariboy

+2

什么是'tn'.'end' – James

回答

0

我已经找到了SolutionphpMyadminServer version: 10.1.13-MariaDB。如果您遇到类似问题,请升级phpMyadmin

现在我能执行我的原始查询

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` 
1

我看到两个语法错误:

  1. when在你打开一个括号这是不是在CASE声明有效。您在AS之前关闭括号,因此您应该从那里删除它。
  2. a case声明以end结尾,而您没有声明。以前as

例所说的那样:

CASE option 
    WHEN condition THEN statement 
    [ELSE] statement 
END 

而且,你的第一个查询似乎是一个有效的MySQL查询。你为什么不使用那个?

+0

请@CynicalSection,请你转换上面的代码,因为我没有太多的知识'mysql' – EaBangalore

0

相反案例的使用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; 
相关问题