Thread: Left outer join with WHERE clause?

Left outer join with WHERE clause?

From
DI Hasenöhrl
Date:
 
Hi all,
 
I have a difficult query with some INNER JOINs and LEFT JOINs.
My first questions is: Is it possible to use a WHERE clause in a LEFT JOIN or can I realize this in a SUBSELECT and
my second question is: please, can anyone show me the correct syntax for this ;-)
 
my example:
SELECT artikel.a_nr, artikel.kl_ean, artikel.kl_nr, artikel.a_aktiv, artikel.a_bez1,artikel.a_bez2,artikel.a_bez3, 
        artikel.wg_nr, artikel.ws_nr, artikel.a_vol,vartikelws.ws_bez,vartikelws.farbe_bez,warengruppebez.wg_bez,                                   warengruppebez.sp_id, produktgruppebez.pg_bez, produktgruppebez.sp_id, kontingent.sortiment,
        kontingent.k_ep, kontingent.k_aufproz, kontingent.k_aufwert,artikel.a_pfand,kontingent.k_vk, warengruppe.pg_id                          FROM (((((artikel INNER JOIN warengruppe ON artikel.wg_nr = warengruppe.wg_nr)
                  INNER JOIN warengruppebez ON warengruppe.wg_nr = warengruppebez.wg_nr) 
                  LEFT JOIN vartikelws ON artikel.ws_nr = vartikelws.ws_nr)
                  INNER JOIN kontingent ON artikel.a_nr = kontingent.a_nr)
                  LEFT JOIN (SELECT lief_nr,kl_kurzbez FROM vartikellieferantbez
                  WHERE ((f_nr)=1) ON artikel.a_nr = vartikellieferantbez.a_nr )             *this does not work
 
                  INNER JOIN produktgruppebez ON warengruppe.pg_id = produktgruppebez.pg_id 
                  WHERE (((warengruppebez.sp_id)='D') AND ((produktgruppebez.sp_id)='D') 
                  AND ((kontingent.sortiment)='2001') AND ((artikel.ws_nr)=1));
      
It doesn't work too:            
     LEFT JOIN (vartikellieferantbez ON artikel.a_nr = vartikellieferantbez.a_nr WHERE ((vartikellieferantbez.f_nr)=1)
 
Please can anyone give me a hint.
Many thanks in advance
Irina
 
 
 
 
 

Re: Left outer join with WHERE clause?

From
"Dan Langille"
Date:
[resent with a valid FROM: address]

On 28 Mar 2002 at 13:58, DI Hasenöhrl wrote:

> It doesn't work too:            LEFT JOIN (vartikellieferantbez ON
> artikel.a_nr = vartikellieferantbez.a_nr WHERE
> ((vartikellieferantbez.f_nr)=1)

Your "where" clause for an OUTER JOIN is actually the ON statement.  So do
this:

LEFT JOIN (vartikellieferantbez ON (artikel.a_nr =
vartikellieferantbez.a_nr  AND vartikellieferantbez.f_nr = 1))
--
Dan Langille
The FreeBSD Diary - http://freebsddiary.org/ - practical examples



Re: Left outer join with WHERE clause?

From
DI Hasenöhrl
Date:
Thank you very much, for your advice, but something is still wrong, because it doesn't work.
 
I get the following message: parse error at or near "on"
 
I tried the Left Join with an easier query, but it still doesn't work. Maybe you see, what I'm making wrong
select artikel.a_nr,artikel.a_bez1,artikel.a_bez2 from Artikel
LEFT JOIN (vartikellieferantbez ON (artikel.a_nr = vartikellieferantbez.a_nr AND vartikellieferantbez.f_nr=1 ));
 
Many thanks,
Irina
----- Original Message -----
Sent: Thursday, March 28, 2002 2:10 PM
Subject: Re: [SQL] Left outer join with WHERE clause?

On 28 Mar 2002 at 13:58, DI Hasenöhrl wrote:

> It doesn't work too:            LEFT JOIN (vartikellieferantbez ON
> artikel.a_nr = vartikellieferantbez.a_nr WHERE
> ((vartikellieferantbez.f_nr)=1)

Your "where" clause for an OUTER JOIN is actually the ON statement.  So do this: 

LEFT JOIN (vartikellieferantbez ON (artikel.a_nr = vartikellieferantbez.a_nr  AND
vartikellieferantbez.f_nr = 1)) 

Re: Left outer join with WHERE clause?

From
DI Hasenöhrl
Date:
Sorry, I found my mistake. It was an incorrect parenthesis.
 
Thanks again
Irina
----- Original Message -----
Sent: Thursday, March 28, 2002 2:10 PM
Subject: Re: [SQL] Left outer join with WHERE clause?

On 28 Mar 2002 at 13:58, DI Hasenöhrl wrote:

> It doesn't work too:            LEFT JOIN (vartikellieferantbez ON
> artikel.a_nr = vartikellieferantbez.a_nr WHERE
> ((vartikellieferantbez.f_nr)=1)

Your "where" clause for an OUTER JOIN is actually the ON statement.  So do this: 

LEFT JOIN (vartikellieferantbez ON (artikel.a_nr = vartikellieferantbez.a_nr  AND
vartikellieferantbez.f_nr = 1)) 

Re: Left outer join with WHERE clause?

From
"Josh Berkus"
Date:
DI,

> I tried the Left Join with an easier query, but it still doesn't
>  work. Maybe you see, what I'm making wrong
>   select artikel.a_nr,artikel.a_bez1,artikel.a_bez2 from Artikel 
>   LEFT JOIN (vartikellieferantbez ON (artikel.a_nr =
>  vartikellieferantbez.a_nr AND vartikellieferantbez.f_nr=1 ));

Get rid of the extra parentheses:
  select artikel.a_nr,artikel.a_bez1,artikel.a_bez2 from Artikel   LEFT JOIN vartikellieferantbez ON (artikel.a_nr =
vartikellieferantbez.a_nrAND vartikellieferantbez.f_nr=1 );
 

-Josh Berkus


Re: Left outer join with WHERE clause?

From
"Dan Langille"
Date:
On 28 Mar 2002 at 13:58, DI Hasenöhrl wrote:

> It doesn't work too:            LEFT JOIN (vartikellieferantbez ON
> artikel.a_nr = vartikellieferantbez.a_nr WHERE
> ((vartikellieferantbez.f_nr)=1)

Your "where" clause for an OUTER JOIN is actually the ON statement.  So do this:

LEFT JOIN (vartikellieferantbez ON (artikel.a_nr = vartikellieferantbez.a_nr  AND
vartikellieferantbez.f_nr = 1))