pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedhow to do this join ?
3+ messages / 3 participants
[nested] [flat]
* how to do this join ?
@ 2001-04-06 12:10 juerg.rietmann@pup.ch
2001-04-06 14:46 ` Re: how to do this join ? Tom Lane <tgl@sss.pgh.pa.us>
2001-04-06 14:51 ` Re: how to do this join ? Peter Eisentraut <peter_e@gmx.net>
0 siblings, 2 replies; 3+ messages in thread
From: juerg.rietmann@pup.ch @ 2001-04-06 12:10 UTC (permalink / raw)
To: pgsql-sql
Hello there
I have another SQL question. Please see the example :
select *,
(select a_typ from auftrag where a_nr=z_a_nr) as typ,
(select a_t_definition_d from auftrags_typ where a_t_code=typ) as text
from zylinder
I have three tables that I need data from. I'd like to use the <as typ> to
temporary store the kind of auftrag and then use it to get the
definition (clear text) from another table.
The query returns that typ is not known .
How can I do it ?
Thanks ... jr
============================================
PFISTER + PARTNER, SYSTEM - ENGINEERING AG
Juerg Rietmann
Grundstrasse 22a
6343 Rotkreuz
Switzerland
phone: +4141 790 4040
fax: +4141 790 2545
mobile: +4179 211 0315
============================================
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: how to do this join ?
2001-04-06 12:10 how to do this join ? juerg.rietmann@pup.ch
@ 2001-04-06 14:46 ` Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 3+ messages in thread
From: Tom Lane @ 2001-04-06 14:46 UTC (permalink / raw)
To: juerg.rietmann@pup.ch; +Cc: pgsql-sql
juerg.rietmann@pup.ch writes:
> select *,
> (select a_typ from auftrag where a_nr=z_a_nr) as typ,
> (select a_t_definition_d from auftrags_typ where a_t_code=typ) as text
> from zylinder
Seems like a very non-SQLish way to proceed. Instead use joins:
select zylinder.*, a_typ as typ, a_t_definition_d as text
from zylinder, auftrag, auftrags_typ
where a_nr = z_a_nr and a_t_code = a_typ
If there were multiple matches in auftrag or auftrags_typ then this
would yield multiple rows per zylinder row, which you might not want;
but your subselect-based approach is already assuming there are not
multiple matches.
If there's a possibility of *no* matching row, then the first solution
would emit NULLs for the missing auftrag and auftrag_typ values, whereas
the second would emit nothing at all for that zylinder row. If that's
not what you want, you need to use outer joins (new in 7.1):
select zylinder.*, a_typ as typ, a_t_definition_d as text
from (zylinder left join auftrag on (a_nr = z_a_nr))
left join auftrags_typ on (a_t_code = a_typ);
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: how to do this join ?
2001-04-06 12:10 how to do this join ? juerg.rietmann@pup.ch
@ 2001-04-06 14:51 ` Peter Eisentraut <peter_e@gmx.net>
1 sibling, 0 replies; 3+ messages in thread
From: Peter Eisentraut @ 2001-04-06 14:51 UTC (permalink / raw)
To: juerg.rietmann@pup.ch; +Cc: pgsql-sql
juerg.rietmann@pup.ch writes:
> Hello there
>
> I have another SQL question. Please see the example :
>
> select *,
> (select a_typ from auftrag where a_nr=z_a_nr) as typ,
> (select a_t_definition_d from auftrags_typ where a_t_code=typ) as text
> from zylinder
select zylinder.*,
auftrag.a_typ
(select a_t_definition_d from auftrags_typ where a_t_code = auftrag.a_typ)
from zylinder, auftrag
where auftrag.a_nr = zylinder.z_a_nr;
or, using 7.1, maybe something like
select zylinder.*, auftrag.a_typ, auftrags_typ.a_t_definition
from (zylinder join auftrag on a_nr = z_a_nr)
left join auftrags_typ on a_t_code = a_typ
Other variations are possible, depending on the referential contraints you
have between the tables.
>
> I have three tables that I need data from. I'd like to use the <as typ> to
> temporary store the kind of auftrag and then use it to get the
> definition (clear text) from another table.
>
> The query returns that typ is not known .
--
Peter Eisentraut peter_e@gmx.net http://yi.org/peter-e/
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2001-04-06 14:51 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-04-06 12:10 how to do this join ? juerg.rietmann@pup.ch
2001-04-06 14:46 ` Tom Lane <tgl@sss.pgh.pa.us>
2001-04-06 14:51 ` Peter Eisentraut <peter_e@gmx.net>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox