this is not a real example, it is something that has been bugging me for some time, and I’d be very thankful to anyone who can explain this behavior.
I have these two tables
MYTABLE1
ID TYPE NAME
1 1 typea
2 2 typea
3 3 typea
4 4 typeb
5 5 typeb
6 6 typeb
7 7 typec
8 8 typec
9 9 typec
10 10 typed
MYTYPE
ID NAME DESCRIPTION
1 typea typea with desc
2 typea
3 typea TYPE
4 typeb typeb with desc
5 typeb
6 typeb TYPE
7 typec typec with desc
8 typec
9 typec TYPE
I’d like to have a sql that returns rows from both tables (mytable1 a, mytype b) where
a) MYTABLE1.type = MYTYPE.ID and MYTYPE.description=’TYPE’
b) MYTABLE1.type is not in MYTYPE.id
a.id a.type a.name b.id b.name b.description
3 3 typea 3 typea TYPE
6 6 typeb 6 typeb TYPE
9 9 typec 9 typec TYPE
10 10 typed null null null
I have tried this statements with no success. I want a solution that uses outer joins, not unions or nested selects.
For the example, I am using Oracle outer join syntax but I think the same result can be achieved by using standard syntax and putting the condition a) inside the ON clause or b)in the where clause
What I’d like is to understand the “strange” behaviour of them, and try to find one that works for the example provided.
Most strange for me it is SQL2. I am not writing the results of the queries, to keep the question shorter, but I can provide them if needed.
SQL1
select *
from
mytable1 a,
mytype b
where
a.type=b.id(+)
and b.description ='TYPE'
order by a.id
SQL2
select *
from
mytable1 a,
mytype b
where
a.type=b.id(+)
and b.description(+) ='TYPE'
order by a.id
SQL3
select *
from
mytable1 a,
mytype b
where
a.type=b.id(+)
and (b.description ='TYPE' or b.description is null)
order by a.id
Thanks in advance,
Stop using old Cartesian product syntax.
JOINsyntax is the ANSI-92 standard. 20 years should be enough to be considered stable…NOTE: I did have
b.description IS NULLbut, as far as I recall, ORACLE treats 0 length strings as NULL. Therefore it is better to test theidfield for a case of No Join.