找不到SQL语法错误

时间:2016-04-28 17:12:17

标签: mysql 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

我无法解决查询中错误的位置

4 个答案:

答案 0 :(得分: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 

您必须在该计算上使用许多左括号,这样可以提前关闭子选择。

答案 1 :(得分: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 

答案 2 :(得分: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 

答案 3 :(得分:-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?从看似不完整的整体查询来看,很难说清楚。