2013-10-25 2 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 

나는 오른쪽으로 테이블을 조인 시도하고 LEFT는 차이가 있는지 확인하기 위해 조인 그러나 그들은 나에게 같은 오류를 제공합니다. 다른 쿼리가 잘 작동하지만,이 쿼리에서이 쿼리를 함께 사용하면 구문 오류가 발생합니다. 누구든지 내가해야하거나하지 말아야 할 아이디어가 있습니까?

오류 : "# 1064 - 당신은 당신의 SQL 구문에 오류가있다; 라인 21 ''근처 사용할 수있는 권리 구문에 대한 MySQL 서버 버전에 해당하는 설명서를 확인을"여기

+1

당신이 집계'SUM'를 사용하기 때문에'그룹 BY'을 변경하지만, 큰은'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

아보다 더 많은 열을 선택해야 b' 나는'하위 쿼리 생각에 대한 참조! 두 번째 옵션이 실제로 효과가있었습니다. 나는 왜 실제로 궁금해하니? 조금 설명해 주시겠습니까? – user2886735

+0

@ user2886735 멀티 조인이라고합니다. 여기에 좀 더 설명이 있습니다 http://stackoverflow.com/questions/2366780/how-to-do-an-inner-join-on-multiple-columns/2366820#2366820 –

+0

@ user2886735 여기에 열이 합쳐집니다. ON 절을 사용하여. :) –