pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedOracle outer join porting question
5+ messages / 3 participants
[nested] [flat]
* Oracle outer join porting question
@ 2003-01-15 14:31 Marko Asplund <aspa@kronodoc.fi>
0 siblings, 1 reply; 5+ messages in thread
From: Marko Asplund @ 2003-01-15 14:31 UTC (permalink / raw)
To: pgsql-sql
i'm trying to port an existing application from Oracle8i to PostgreSQL but
i'm having problems understanding a certain outer join query type used in
the application. the query includes a normal outer join between two tables
but also uses outer join syntax to join a table with a constant. here's a
simplified version of the query:
SELECT doc.id,doc.title,sub.user_id,sub.operation
FROM document doc, document_subscription sub
WHERE 6 = sub.user_id(+) AND sub.document_id(+) = doc.id;
what does the '6 = sub.user_id(+)' condition exactly do in this query?
how would this be translated SQL92 join syntax used by PostgreSQL?
i've tried converting it to:
SELECT doc.id,doc.title,sub.user_id,sub.operation
FROM document doc LEFT OUTER JOIN document_subscription sub
ON sub.document_id = doc.id
WHERE (sub.user_id = 6 OR sub.user_id IS NULL);
but this query is missing the rows in the documents table which have a
corresponding document_subscription row with 'not user_id = 6'.
here're also simplified definitions of the two tables used in the query
and some test data:
CREATE TABLE document (
id INTEGER,
title VARCHAR(100),
PRIMARY KEY(id)
);
CREATE TABLE document_subscription (
document_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
operation VARCHAR(10)
);
INSERT INTO document VALUES (1, 'doc1');
INSERT INTO document VALUES (2, 'doc2');
INSERT INTO document VALUES (4, 'doc4');
INSERT INTO document_subscription VALUES (1, 5, 'op1');
INSERT INTO document_subscription VALUES (2, 5, 'op2');
INSERT INTO document_subscription VALUES (2, 6, 'op2');
best regards,
--
aspa http://www.kronodoc.fi/
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Oracle outer join porting question
@ 2003-01-15 14:38 Dan Langille <dan@langille.org>
parent: Marko Asplund <aspa@kronodoc.fi>
0 siblings, 0 replies; 5+ messages in thread
From: Dan Langille @ 2003-01-15 14:38 UTC (permalink / raw)
To: Marko Asplund <aspa@kronodoc.fi>; +Cc: pgsql-sql
On 15 Jan 2003 at 16:31, Marko Asplund wrote:
>
> i'm trying to port an existing application from Oracle8i to PostgreSQL but
> i'm having problems understanding a certain outer join query type used in
> the application. the query includes a normal outer join between two tables
> but also uses outer join syntax to join a table with a constant. here's a
> simplified version of the query:
>
> SELECT doc.id,doc.title,sub.user_id,sub.operation
> FROM document doc, document_subscription sub
> WHERE 6 = sub.user_id(+) AND sub.document_id(+) = doc.id;
>
> what does the '6 = sub.user_id(+)' condition exactly do in this query?
> how would this be translated SQL92 join syntax used by PostgreSQL?
>
> i've tried converting it to:
>
> SELECT doc.id,doc.title,sub.user_id,sub.operation
> FROM document doc LEFT OUTER JOIN document_subscription sub
> ON sub.document_id = doc.id
> WHERE (sub.user_id = 6 OR sub.user_id IS NULL);
>
> but this query is missing the rows in the documents table which have a
> corresponding document_subscription row with 'not user_id = 6'.
What about this:
SELECT doc.id,doc.title,sub.user_id,sub.operation
FROM document doc LEFT OUTER JOIN document_subscription sub
ON sub.document_id = doc.id;
id | title | user_id | operation
----+-------+---------+-----------
1 | doc1 | 5 | op1
2 | doc2 | 5 | op2
2 | doc2 | 6 | op2
4 | doc4 | |
(4 rows)
>
> here're also simplified definitions of the two tables used in the query
> and some test data:
Thanks for supplying the table and data. That makes things much
easier.
--
Dan Langille : http://www.langille.org/
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Oracle outer join porting question
@ 2003-01-15 14:40 Tambet Matiisen <t.matiisen@aprote.ee>
0 siblings, 2 replies; 5+ messages in thread
From: Tambet Matiisen @ 2003-01-15 14:40 UTC (permalink / raw)
To: Marko Asplund <aspa@kronodoc.fi>; pgsql-sql
> -----Original Message-----
> From: Marko Asplund [mailto:aspa@kronodoc.fi]
> Sent: Wednesday, January 15, 2003 4:31 PM
> To: pgsql-sql@postgresql.org
> Subject: [SQL] Oracle outer join porting question
>
>
>
> i'm trying to port an existing application from Oracle8i to
> PostgreSQL but
> i'm having problems understanding a certain outer join query
> type used in
> the application. the query includes a normal outer join
> between two tables
> but also uses outer join syntax to join a table with a
> constant. here's a
> simplified version of the query:
>
> SELECT doc.id,doc.title,sub.user_id,sub.operation
> FROM document doc, document_subscription sub
> WHERE 6 = sub.user_id(+) AND sub.document_id(+) = doc.id;
>
> what does the '6 = sub.user_id(+)' condition exactly do in
> this query?
> how would this be translated SQL92 join syntax used by PostgreSQL?
>
> i've tried converting it to:
>
> SELECT doc.id,doc.title,sub.user_id,sub.operation
> FROM document doc LEFT OUTER JOIN document_subscription sub
> ON sub.document_id = doc.id
> WHERE (sub.user_id = 6 OR sub.user_id IS NULL);
>
> but this query is missing the rows in the documents table which have a
> corresponding document_subscription row with 'not user_id = 6'.
>
Try this:
SELECT doc.id,doc.title,sub.user_id,sub.operation
FROM document doc LEFT OUTER JOIN document_subscription sub
ON sub.document_id = doc.id AND sub.user_id = 6;
Tambet
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Oracle outer join porting question
@ 2003-01-15 15:01 Dan Langille <dan@langille.org>
parent: Tambet Matiisen <t.matiisen@aprote.ee>
1 sibling, 0 replies; 5+ messages in thread
From: Dan Langille @ 2003-01-15 15:01 UTC (permalink / raw)
To: Marko Asplund <aspa@kronodoc.fi>; +Cc: pgsql-sql
On 15 Jan 2003 at 16:40, Tambet Matiisen wrote:
> Try this:
>
> SELECT doc.id,doc.title,sub.user_id,sub.operation
> FROM document doc LEFT OUTER JOIN document_subscription sub
> ON sub.document_id = doc.id AND sub.user_id = 6;
FWIW:
test=# SELECT doc.id,doc.title,sub.user_id,sub.operation
test-# FROM document doc LEFT OUTER JOIN document_subscription sub
test-# ON sub.document_id = doc.id AND sub.user_id = 6;
id | title | user_id | operation
----+-------+---------+-----------
1 | doc1 | |
2 | doc2 | 6 | op2
4 | doc4 | |
--
Dan Langille : http://www.langille.org/
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Oracle outer join porting question
@ 2003-01-16 11:32 Marko Asplund <aspa@kronodoc.fi>
parent: Tambet Matiisen <t.matiisen@aprote.ee>
1 sibling, 0 replies; 5+ messages in thread
From: Marko Asplund @ 2003-01-16 11:32 UTC (permalink / raw)
To: Tambet Matiisen <t.matiisen@aprote.ee>; +Cc: pgsql-sql
On Wed, 15 Jan 2003, Tambet Matiisen wrote:
> ...
> Try this:
>
> SELECT doc.id,doc.title,sub.user_id,sub.operation
> FROM document doc LEFT OUTER JOIN document_subscription sub
> ON sub.document_id = doc.id AND sub.user_id = 6;
yes, this query seems to give the same results as the original one.
thanks!
best regards,
--
aspa http://www.kronodoc.fi/
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2003-01-16 11:32 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-01-15 14:31 Oracle outer join porting question Marko Asplund <aspa@kronodoc.fi>
2003-01-15 14:38 ` Dan Langille <dan@langille.org>
2003-01-15 14:40 Re: Oracle outer join porting question Tambet Matiisen <t.matiisen@aprote.ee>
2003-01-15 15:01 ` Dan Langille <dan@langille.org>
2003-01-16 11:32 ` Marko Asplund <aspa@kronodoc.fi>
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