在进行子查询时,为什么会出现MySQL语法错误?

时间:2015-04-15 04:08:30

标签: mysql sql subquery

以下是我的查询,需要从子查询生成视图,然后在顶部运行数学。我运行时遇到语法错误。我以前做过子查询,所以我可能只是累了,看不到问题。

SELECT  STDDEV(north.value)AS north, 
        STDDEV(south.value)AS south, 
        STDDEV(west.value) AS west, 
        STDDEV(east.value) AS east 
FROM ( 
        SELECT `series_id`, 
                value 
        FROM   `current` 
        WHERE (( 
                    SUBSTRING(series_id,5,4) = '0100' 
                AND SUBSTRING(series_id,9,8) = 'SETA' 
                AND theyear>= (2000) 
                AND theyear <=(2012) 
                AND period = 'M13') as north 
            OR ( 
                    substring(series_id,5,4) = '0200' 
                AND substring(series_id,9,8) = 'SETA' 
                AND theyear>= (2000) 
                AND theyear <=(2012) 
                AND period = 'M13') AS south 
            OR ( 
                    substring(series_id,5,4) = '0300' 
                AND substring(series_id,9,8) = 'SETA' 
                AND theyear>= (2000) 
                AND theyear <=(2012) 
                AND period = 'M13') AS west 
            OR ( 
                    substring(series_id,5,4) = '0400' 
                AND substring(series_id,9,8) = 'SETA' 
                AND theyear>= (2000) 
                AND theyear <=(2012) 
                AND period = 'M13') AS east
            )
  

1064 - 您的SQL语法出错;检查与您的MySQL服务器版本对应的手册,以便在'AS north OR附近使用正确的语法               (                 SUBSTRING(series_id,5,4)='0200'AND     '第17行

2 个答案:

答案 0 :(得分:0)

我开始重新格式化:

SELECT STDDEV(north.value)AS North,
       STDDEV(south.value)as South,
       STDDEV(west.value) as West,
       STDDEV(east.value) AS East

FROM (
    SELECT `series_id`, 
            value
      FROM `CURRENT`
     WHERE (
            (
              SUBSTRING(series_id,5,4) = '0100' AND
              SUBSTRING(series_id,9,8) = 'SETA' AND
              theYear>= (2000) AND
              theYear <=(2012) AND
              period = 'M13'
            ) AS north OR
            (
              SUBSTRING(series_id,5,4) = '0200' AND
              SUBSTRING(series_id,9,8) = 'SETA' AND
              theYear>= (2000) AND
              theYear <=(2012) AND
              period = 'M13'
            ) as south OR
            (
            SUBSTRING(series_id,5,4) = '0300' AND
            SUBSTRING(series_id,9,8) = 'SETA' AND
            theYear>= (2000) AND
            theYear <=(2012) AND
            period = 'M13'
            ) as west OR
            (
            SUBSTRING(series_id,5,4) = '0400' AND
            SUBSTRING(series_id,9,8) = 'SETA' AND
            theYear>= (2000) AND
            theYear <=(2012) AND 
            period = 'M13'
            ) as east
          )

看起来您缺少终止子查询的括号。你也应该添加一个别名......像这样:

) subquery 

这只是我的头脑。我无法在我目前使用的机器上运行MySQL。此外,您没有附加实际的语法错误消息,这也很难。

还要注意&#34;值&#34;和&#34;当前&#34;是reserved words in many DBMS and the SQL standard虽然它们不在MySQL中,但我仍然会尝试为字段/列选择更好的名称。这样可以更轻松地迁移您的架构,也可以在未来对其进行验证。

编辑:但是,在您的内部查询中,您只选择series_idvalue而不是north.valuesouth.valuewest.value,{{1} }。所以外部查询仍然会失败。查询中存在结构性问题,因为这4个字段应该从条件移动到内部查询的SELECT部分​​。

答案 1 :(得分:0)

SELECT  `series_id`, FORMAT(STDDEV(Q2.value),5) 
FROM ( 
    SELECT `series_id`, 
            `value`,
            `theYear`
    FROM   `CURRENT` 
    WHERE (( 
                SUBSTRING(series_id,5,4) = '0100' 
            AND SUBSTRING(series_id,9,8) = 'SETA' 
            AND theyear>= (2000) 
            AND theyear <=(2012) 
            AND period = 'M13') 
        OR ( 
                substring(series_id,5,4) = '0200' 
            AND substring(series_id,9,8) = 'SETA' 
            AND theyear>= (2000) 
            AND theyear <=(2012) 
            AND period = 'M13')  
        OR ( 
                substring(series_id,5,4) = '0300' 
            AND substring(series_id,9,8) = 'SETA' 
            AND theyear>= (2000) 
            AND theyear <=(2012) 
            AND period = 'M13') 
        OR ( 
                    substring(series_id,4,1) = 'R'
            AND    substring(series_id,5,4) = '0400' 
            AND substring(series_id,9,8) = 'SETA' 
            AND theyear>= (2000) 
            AND theyear <=(2012)
            )
           )
          )     AS Q2
          GROUP BY `series_id`