X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id B38F0D1B502; Tue, 14 Oct 2003 14:35:45 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 09184-05; Tue, 14 Oct 2003 11:35:18 -0300 (ADT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id 4589AD1B50D; Tue, 14 Oct 2003 11:35:12 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.10/8.12.10) with ESMTP id h9EEZ6hn009191; Tue, 14 Oct 2003 10:35:07 -0400 (EDT) To: newsy@lewczuk.com Cc: "'Karsten Hilbert'" , "'Lista dyskusyjna pgsql-general'" , "Lista dyskusyjna pgsql-sql" Subject: Re: [SQL] SQL query problem (many table in FROM statement and many LEFT JOIN's) In-reply-to: <004001c38f03$8d8b8230$4701a8c0@marek> References: <004001c38f03$8d8b8230$4701a8c0@marek> Comments: In-reply-to "Marek Lewczuk" message dated "Fri, 10 Oct 2003 08:53:11 +0100" Date: Tue, 14 Oct 2003 10:35:06 -0400 Message-ID: <9190.1066142106@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/655 X-Sequence-Number: 50728 "Marek Lewczuk" writes: > ... It also working fine. !!!! The question is, why my first query isn't > working: > SELECT > _CON.con_id, > _MOD.mod_ty, > _VER.version, > _YEA.year, > _CON.dri_id, > _CON.man_cod, > _ENG.eng_pow > FROM > db_data.mda_mod _MOD, > db_data.mda_mod_con _CON, > db_data.mda_mak_eng _ENG, > db_data.set_mda_fue _FUE > LEFT JOIN db_data.mda_mod_ver _VER ON _VER.ver_id = _CON.ver_id > LEFT JOIN db_data.mda_mod_yea _YEA ON _YEA.yea_id = _CON.yea_id > WHERE > _MOD.mod_id = '283' AND > _CON.mod_id = _MOD.mod_id AND > _CON.psd <= NOW() AND > _CON.ped > NOW() AND > _ENG.eng_id = _CON.eng_id AND > _ENG.eng_fue = _FUE.fue_id The reason that works in MySQL and fails in Postgres is that MySQL isn't compliant with the SQL standard. The standard says that the above FROM clause means that _FUE is left-joined to _VER, then that result is left-joined to _YEA, then the _MOD, _CON, and _ENG tables are joined to that result (in no particular order). You get the error because the LEFT JOIN ON clauses refer to _CON which is not part of what they are joining. I believe that MySQL interprets the above statement as "join the tables in the order listed in the FROM clause", that is they join _MOD/_CON/_ENG/_FUE, then left-join _VER to that result, etc. This is a hangover from days when they didn't actually have a query planner. Unfortunately, it's not SQL, it's only something that looks like SQL. regards, tom lane