Re: Problem with joining two tables

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Problem with joining two tables
Date: 2007-12-05 14:02:08
Message-ID: 20071205140208.GI24799@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

am Wed, dem 05.12.2007, um 14:42:32 +0100 mailte Przemyslaw Bojczuk folgendes:
> Hello!
>
> I have a problem joining two tables. I tried various types of join and
> none seems to work as I expect
>
> Table 1:
>
> id | stuff
> -----------
> 1 | sth1
> 2 | sth2
> 3 | sth3
> 4 | sth4
> 5 | sth5
> .. | ...
>
> Table 2:
>
> id | desc | etc
> ------------------
> 1 | desc1 | etc1
> 2 | desc2 | etc2
> 2 | desc3 | etc3
> 2 | desc4 | etc4
> 3 | desc5 | etc5
> | desc6 | etc6
> 5 | desc7 | etc7
> .. | ... | ...
>
> I need something like:
>
> id | stuff | desc | etc
> -------------------------
> 1 | sth1 | desc1 | etc1
> 2 | sth2 | desc2 | etc2
> 2 | sth2 | desc3 | etc3
> 2 | sth2 | desc4 | etc4
> 3 | sth3 | desc5 | etc5
> 5 | sth5 | desc7 | etc7
>

Okay, let's try:

first i create your tables like above:

test=*# select * from t1;
id | stuff
----+-------
1 | sth1
2 | sth2
3 | sth3
4 | sth4
5 | sth5
(5 rows)

test=*# select * from t2;
id | des | etc
----+-------+------
1 | desc1 | etc1
2 | desc2 | etc2
2 | desc3 | etc3
2 | desc4 | etc4
3 | desc5 | etc5
| desc6 | etc6
5 | desc7 | etc7
(7 rows)

And now:

test=*# select t1.id, t1.stuff, t2.des, t2.etc from t1, t2 where t1.id=t2.id;
id | stuff | des | etc
----+-------+-------+------
1 | sth1 | desc1 | etc1
2 | sth2 | desc2 | etc2
2 | sth2 | desc3 | etc3
2 | sth2 | desc4 | etc4
3 | sth3 | desc5 | etc5
5 | sth5 | desc7 | etc7
(6 rows)

is this your expected result?

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Przemyslaw Bojczuk 2007-12-05 14:13:09 Re: Problem with joining two tables
Previous Message Thomas Burdairon 2007-12-05 14:00:03 Re: Problem with joining two tables