2013-10-25 28 views
0

我只是無法弄清楚這個語法有什麼問題。最後一行出現#1064語法錯誤。另一個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 

我試圖聯接表與左,右連接來看看是否是有差別的,但他們給我同樣的錯誤。 工作中的不同查詢可以很好地處理它們,但是當我在查詢中將它們連接在一起時,我得到一個語法錯誤。有誰知道我應該做什麼或不應該做什麼?

的錯誤:「#1064 - 你在你的SQL語法錯誤;檢查對應於您的MySQL服務器版本在第21行使用‘’附近正確語法手冊」

+1

我想在子查詢'B'您需要更改'GROUP BY'因爲你使用聚合'SUM'但選擇比'GROUP BY' – JScoobyCed

回答

0

這裏是參考了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 
+0

偉大啊多個列!第二種選擇確實奏效。我想知道爲什麼呢?如果你想解釋一下? – user2886735

+0

@ user2886735它被稱爲multijoin。這裏有一點更多的澄清在這裏http://stackoverflow.com/questions/2366780/how-to-do-an-inner-join-on-multiple-columns/2366820#2366820 –

+0

@ user2886735加入是根據列在哪裏通過使用ON子句。 :) –