From 8f0b025fca62e2c1ba05c53a2b51dad3ac5f7fde Mon Sep 17 00:00:00 2001 From: Stefan Koch Date: Tue, 30 Jul 2013 11:05:00 +0200 Subject: [PATCH] corrected SQL statement - it's totally uneeded to join the FROM table once again - I do not know about specifying multiple tables in one JOIN, it might be wrong, but at least it's (same as using CROSS JOIN syntax with multiple FROM tables) at least bad behaviour - omitting the ON clause when using JOIN is afaik not optional in the SQL standard. MySQL allows it, but then uses CROSS JOIN. Here you either want NATURAL JOIN or JOIN with ON clause. I prefer JOIN with explicit ON clauses all the time (because I never call my columns Berater.berater_id, but Berater.id and in foreign key definitions then Foo.berater_id), but since in the lectures NATURAL JOIN was so often prefered, I thought about using that. In my own exam, I will always use ON clause, because I am more used to that and I will not run into troubles if there are two columns with the same name, which is exactly the problem here. There are two tables with column "name". The second NATURAL JOIN would fail, because it would try to not only JOIN Kunden2Berater.kunden_id with Kunden.kunden_id, but also Berater.name with Kunden.name. Thus, I'd recommend to always use explicit ON statements. --- documents/musterloesung-db-klausur-b/d3c.sql | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/documents/musterloesung-db-klausur-b/d3c.sql b/documents/musterloesung-db-klausur-b/d3c.sql index 9671b49..4150259 100644 --- a/documents/musterloesung-db-klausur-b/d3c.sql +++ b/documents/musterloesung-db-klausur-b/d3c.sql @@ -1,3 +1,4 @@ -SELECT name FROM Berater - JOIN Kunden, Kunden2Berater, Berater - WHERE Kunden.name = "Müller" +SELECT name FROM Berater b + JOIN Kunden2Berater kb ON kb.berater_id = b.berater_id + JOIN Kunden k ON k.kunden_id = kb.kunden_id + WHERE k.name = "Müller"