2017-06-12 72 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; 
相關問題