我是SQL新手,我正在使用JDBC。现在我有一些似乎是语法错误的东西,但我似乎无法找到问题......错误是
DBConnection.executeSelectQuery()中的SQL异常SQLException:您 您的SQL语法有错误;检查对应的手册 您的MySQL服务器版本,以便在'a附近使用正确的语法,(SELECT a.id AS id,a.startpunkte + t1.pkte AS pkte FROM arzt a,(SELECT a.i' 在第1行SQLState:42000 VendorError:1064
而我的陈述是
SELECT a.id, a.ansage_id, a.name, a.vorname, a.strasse, a.hausnummer, a.stadt, a.aktiv, a.wertung, t.pkte
FROM arzt a, (SELECT a1.id AS id, a1.startpunkte+t1.pkte AS pkte
FROM arzt a1, (SELECT a2.id, SUM(t.punkte) AS pkte
FROM arzt a2, dienst d, tag t
WHERE d.arzt_id = a2.id
AND t.id = d.tag_id
) AS t1
WHERE t1.id=a1.id
) AS t
WHERE a.id = t.id
ORDER BY a.name
我希望有人可以帮助我。抱歉我的英语不好。
答案 0 :(得分:0)
您需要按artz.id
对SUM()
进行分组才能正常使用。我还将WHERE
JOINS更改为INNER JOINS
,以提高可读性:
SELECT a.id, a.ansage_id, a.name
FROM arzt a
INNER JOIN
(SELECT a.id AS id, a.startpunkte+t1.pkte AS pkte
FROM arzt a INNER JOIN
(SELECT a.id, SUM(t.punkte) AS pkte
FROM arzt a
INNER JOIN dienst d
ON d.arzt_id = a.id
INNER JOIN tag t
ON t.id = d.tag_id
GROUP BY a.id) AS t1
ON t1.id=a.id ) AS t
ON a.id = t.id
ORDER BY a.name;
正如其他人所提到的,在子查询中重用相同的别名令人困惑。我已经在小提琴中更新了。