954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Excluding result from SELECT statement

Hi,

I have a table called BookTitle that holds book information. I have an SQL statement that lists all books that have the same value of a book called 'Northern Lights'. The code works but what I want to do is to exclude 'Northern Lights' from the result.

This is the code I have:

SELECT bt2.btName
FROM BookTitle bt1, BookTitle bt2
WHERE bt1.btName = 'Northern Lights'
AND bt2.value = bt1.value ;


Any help would be appreciated.

Thanks

blivori
Newbie Poster
12 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

Use != for 'Not equal to'.
Change this

WHERE bt1.btName = 'Northern Lights'

to this

WHERE bt1.btName != 'Northern Lights'
simplypixie
Posting Pro in Training
447 posts since Oct 2010
Reputation Points: 116
Solved Threads: 82
 

I think what you really want is...

SELECT bt2.btName
FROM BookTitle bt2
WHERE bt2.btName != 'Northern Lights'
AND bt2.value =
(Select bt1.value
 From BootTitle bt1
 Where bt1.btName = 'Northern Lights')
Order by Upper (bt2.btName)
;

This should get you what I think you are looking for.

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

How do you know that is what they really want and have therefore completely over-complicated the code the OP posted?

simplypixie
Posting Pro in Training
447 posts since Oct 2010
Reputation Points: 116
Solved Threads: 82
 

Well... I don't know about you, but... I read the requirements!

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

Yes, so did I
The code works but what I want to do is to exclude 'Northern Lights' from the result
Quite simple in their requirements I think

simplypixie
Posting Pro in Training
447 posts since Oct 2010
Reputation Points: 116
Solved Threads: 82
 

Yes... I think the requirements are quite simple.
However... Your modification will return ALL books (even 'Northern Lights'), MULTIPLE times (because of the join to itself).
This does NOT fulfill the requirements the OP posted.

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

Just you have to replace

WHERE bt1.btName != 'Northern Lights'

instead of

WHERE bt1.btName = 'Northern Lights'

easy2teach
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Your answer is the same as simplypixie's answer.
Sorry... But, this will NOT work.

hfx642
Posting Pro
515 posts since Nov 2009
Reputation Points: 248
Solved Threads: 105
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: