| From: | Przemyslaw Bojczuk <pb2(at)gis(dot)umcs(dot)lublin(dot)pl> | 
|---|---|
| To: | pgsql-general(at)postgresql(dot)org | 
| Subject: | Problem with joining two tables | 
| Date: | 2007-12-05 13:42:32 | 
| Message-ID: | 20071205144232.65fcb8ec.pb2@gis.umcs.lublin.pl | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-general | 
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
So: join by id, discard rows that don't match any row from the other
table, add separate row for each row from table 2 that matches the same
row from table 1.
So far the best I could get (using inner join) was something like:
 id | stuff | desc  | etc
-------------------------
  1 | sth1  | desc1 | etc1
  2 | sth2  | desc2 | etc2
  2 | sth2  | desc2 | etc2
  2 | sth2  | desc2 | etc2
  3 | sth3  | desc5 | etc5
  5 | sth5  | desc7 | etc7
(i.e. multiplied one row from table 2 instead of separate rows matching
the same row from table 1)
right/left/full (outer) also seem to do the same thing (multiply one
row) and I don't know any other join methods.
Is there a way to accomplish what I am trying to do? Or maybe I am
missing something?
Thanks in advance!
PB
-- 
Geographical Information Systems Laboratory
Institute of Earth Sciences, UMCS
http://gis.umcs.lublin.pl/en/
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Robert Treat | 2007-12-05 13:50:07 | Re: Moving pgstat.stat and pgstat.tmp | 
| Previous Message | Joshua D. Drake | 2007-12-05 13:00:33 | Re: [GENERAL] PostgreSQL Beta4 released |