【问题标题】:Another 1064 syntax error另一个 1064 语法错误
【发布时间】:2013-11-04 10:52:00
【问题描述】:

我只是想不通这种语法有什么问题。最后一行出现 #1064 语法错误。

SELECT b.id, b.lastname, b.name, c.balance, a.maxdebt, b.warndata, b.warndownload, b.warnupload, b.warndebt, b.cutoffdata, b.cutoffdownload, b.cutoffupload, b.cutoffdebt, b.data, b.download, b.upload, b.warning, b.access, b.cutoffstop
FROM (
SELECT customers.id AS id, SUM(tariffs.value) AS maxdebt
            FROM tariffs
            INNER JOIN assignments ON tariffs.id = assignments.tariffid
            INNER JOIN customers ON assignments.customerid = customers.id
            GROUP BY id
) a
LEFT JOIN (
SELECT customers.id AS id, UPPER(lastname) AS lastname, customers.name AS name, SUM(stats.upload+stats.download) AS data, SUM(stats.download) AS download, SUM(stats.upload) AS upload, cutoffstop, warndata, warndownload, warnupload, warndebt, cutoffdata, cutoffdownload, cutoffupload, cutoffdebt, warning, access
        FROM customers
        LEFT JOIN nodes ON customers.id = nodes.ownerid
        LEFT JOIN stats ON nodes.id = stats.nodeid
        LEFT JOIN customerwarnings ON customers.id = customerwarnings.id
    GROUP BY id
) b
LEFT JOIN (
SELECT customerid, SUM(cash.value) AS balance
        FROM cash
        GROUP BY customerid
) c ON a.id = b.id = c.customerid

我尝试使用 RIGHT 和 LEFT JOINS 连接表以查看是否存在差异,但它们给了我同样的错误。 内部的不同查询本身可以正常工作,但是当我在此查询中将它们连接在一起时,会出现语法错误。有谁知道我应该做什么或不应该做什么?

错误:“#1064 - 您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,了解在第 21 行的 '' 附近使用的正确语法”

【问题讨论】:

  • 我认为在子查询b 中您需要更改GROUP BY 因为您使用聚合SUM 但选择的列比GROUP BY

标签: mysql sql syntax-error


【解决方案1】:

这是Multiple LEFT JOIN的参考

SELECT b.id, b.lastname, b.name, c.balance, a.maxdebt, b.warndata, b.warndownload, b.warnupload, b.warndebt, b.cutoffdata, b.cutoffdownload, b.cutoffupload, b.cutoffdebt, b.data, b.download, b.upload, b.warning, b.access, b.cutoffstop
FROM (
SELECT customers.id AS id, SUM(tariffs.value) AS maxdebt
            FROM tariffs
            INNER JOIN assignments ON tariffs.id = assignments.tariffid
            INNER JOIN customers ON assignments.customerid = customers.id
            GROUP BY id
) a
LEFT JOIN (
SELECT customers.id AS id, UPPER(lastname) AS lastname, customers.name AS name, SUM(stats.upload+stats.download) AS data, SUM(stats.download) AS download, SUM(stats.upload) AS upload, customers.cutoffstop, warndata, warndownload, warnupload, warndebt, cutoffdata, cutoffdownload, cutoffupload, cutoffdebt, nodes.warning, nodes.access
        FROM customers
        LEFT JOIN nodes ON customers.id = nodes.ownerid
        LEFT JOIN stats ON nodes.id = stats.nodeid
        LEFT JOIN customerwarnings ON customers.id = customerwarnings.id
    GROUP BY id
) b ON a.id = b.id
LEFT JOIN (
SELECT customerid, SUM(cash.value) AS balance
        FROM cash
        GROUP BY customerid
) c ON a.id = c.customerid

【讨论】:

  • 啊太棒了!第二种选择确实有效。我想知道为什么实际上?如果你想解释一下?
  • @user2886735 它被称为多连接。这里有更多的澄清stackoverflow.com/questions/2366780/…
  • @user2886735 通过使用 ON 子句对列进行连接。 :)
猜你喜欢
  • 2011-12-16
  • 1970-01-01
  • 2015-04-25
  • 1970-01-01
  • 1970-01-01
  • 2017-11-29
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多