Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
HELP : ORACLE, "NOT IN" Query
Old 10-05-2009, 05:04 AM HELP : ORACLE, "NOT IN" Query
Junior Talker

Posts: 2
Name: Nobi Nobita
Trades: 0
I Have this query to get land which doesn't have a house on it. Usually this is work on MySQL, but in Oracle, it won't work


"SELECT ID FROM land
WHERE ID NOT IN (SELECT id_land AS ID FROM house)"

Can anyone help, what did I'm wrong ?
nobita110384 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-05-2009, 08:05 AM Re: HELP : ORACLE, "NOT IN" Query
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
maybe "not in" does not what you expect, because oracle can understand it.
Look at this page:
http://jonathanlewis.wordpress.com/2007/02/25/not-in/

You may want to use the "NOT EXISTS" rather than "NOT IN" to do what you want.
Code:
SELECT ID 
FROM land l
WHERE NOT EXISTS (
    SELECT 1
    FROM house h
    WHERE h.id_land=l.ID
)
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 10-05-2009, 08:51 AM Re: HELP : ORACLE, "NOT IN" Query
Junior Talker

Posts: 2
Name: Nobi Nobita
Trades: 0
Thanks for the suggest.

I found that, my query need a little bit changes

"SELECT ID FROM land
WHERE ID NOT IN (SELECT id_land AS ID FROM house WHERE id_land NOT NULL)"

I didn't know why, but the record from 'house' table where id_land equal to null, affected the result of my old query. And now, the result show as I expected.
nobita110384 is offline
Reply With Quote
View Public Profile
 
Old 10-06-2009, 07:47 AM Re: HELP : ORACLE, "NOT IN" Query
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
That's because of the nature of a null value.

A null value is an "unknown" value.
You cannot match it against anything that is not null itself.
And any operation on a null equals null.

The alternate syntax with a not exists eliminate this scenario, because as you do a join between the external query and the inner query, you naturally exclude the null values in the land.id table
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to HELP : ORACLE, "NOT IN" Query
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13368 seconds with 12 queries