2016-04-28 92 views
3

我試圖運行該SQL查詢:找不到SQL語法錯誤

SELECT avg(response_seconds) as s FROM 
    (select time_to_sec(timediff(from_unixtime(floor(UNIX_TIMESTAMP(u.datetime)/60)*60), u.datetime))) as response_seconds 
    FROM tickets t JOIN ticket_updates u ON t.ticketnumber = u.ticketnumber 
    WHERE u.type = 'update' and t.customer = 'Y' and DATE(u.datetime) = '2016-04-18' 
    GROUP BY t.ticketnumber) 
    AS r 

,但我看到這個錯誤:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM tickets t JOIN ticket_updates u ON t.ticketnumber = u.ticketnumber WHE' at line 3 

,我不能工作了錯誤所在查詢

+0

什麼SQL? SQL Server?更新您的標籤。 –

+0

編輯的問題:) – charlie

+6

看起來像你在'as response_seconds'之前太早關閉''''。這屬於'(select ...)'內部,但你在外面。 「在FROM附近使用正確的語法」意味着您必須立即查看該問題才能找到問題。 –

回答

1

)一個更額外的括號中)) as response_seconds導致此問題,刪除,將解決問題。爲方便閱讀,我對齊的代碼:

SELECT avg(response_seconds) AS s 
FROM 
( 
    SELECT 
     time_to_sec( 
      timediff( 
       from_unixtime( 
        floor( 
          UNIX_TIMESTAMP(u.datetime)/60 
         )*60 
       ), u.datetime 
      ) --) the one more extra parenthesis causing the problem 
     ) as response_seconds 
    FROM tickets t 
    JOIN ticket_updates u ON t.ticketnumber = u.ticketnumber 
    WHERE u.type = 'update' and t.customer = 'Y' and DATE(u.datetime) = '2016-04-18' 
    GROUP BY t.ticketnumber 
) AS r 
-1

計算括號。你基本上是這樣做的:

SELECT some_column as s 
FROM some_sub_select as response_seconds 
FROM tickets t JOIN ticket_updates u ON t.ticketnumber = u.ticketnumber 
.... 

在同一查詢中不能有兩個FROM子句。

也許你未對齊的括號?也許你打算做一個JOIN而不是第二個FROM?從看似不完整的整體查詢中很難分辨出來。

2

取出)只是as response_seconds之前

SELECT avg(response_seconds) as s FROM 
    (select time_to_sec(timediff(from_unixtime(floor(UNIX_TIMESTAMP(u.datetime)/60)*60), u.datetime)) as response_seconds 
     FROM tickets t 
     JOIN ticket_updates u ON t.ticketnumber = u.ticketnumber 
     WHERE u.type = 'update' 
     and t.customer = 'Y' 
     and DATE(u.datetime) = '2016-04-18' 
     GROUP BY t.ticketnumber 
    ) AS r 

你必須多收支架上計算了其提前關子選擇的效果。

+0

但是你有'FROM response_seconds'和'FROM tickets'? –

+0

它的一個子查詢,它需要一個FROM – RiggsFolly

+0

哦,現在對齊後,我看你是對的。 +1 –

0

看來你有一個額外的「)」在這裏「tetime)))< - 作爲response_seco」


SELECT avg(response_seconds) as s 
FROM (
     select time_to_sec(timediff(from_unixtime(floor(UNIX_TIMESTAMP(u.datetime)/60)*60), u.datetime)) as response_seconds 
     FROM tickets t JOIN ticket_updates u ON t.ticketnumber = u.ticketnumber 
     WHERE u.type = 'update' and t.customer = 'Y' and DATE(u.datetime) = '2016-04-18' 
     GROUP BY t.ticketnumber 
    ) AS r