If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Newbie help - UNION operator

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-27-10, 18:42
harofreak00 harofreak00 is offline
Registered User
 
Join Date: Apr 2010
Posts: 3
Newbie help - UNION operator

Hey guys, working on a simple query here, but I'm not getting the results I expect. Here is the exact question I am to work:

Quote:
Find the book code and book title for each book whose price is more than $10 or that was published in Boston.
Here is what I came up with:

Code:
SELECT B.BookCode, B.Title
FROM Book B
WHERE B.Price > '10'
UNION
SELECT B.BookCode, B.Title
FROM Book B, Publisher P
WHERE B.PublisherCode = P.PublisherCode;
However, this returns all the Books, even though I shouldn't. What did I do wrong? The chapter I am studying is specifically going over the SET operators (UNION, INTERSECT & MINUS), and the ANY and ALL operators. I figured UNION was closest to the example in the book, so thats why I choose that.
Reply With Quote
  #2 (permalink)  
Old 04-27-10, 19:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
your UNION approach was very good, but it was incomplete

in the second of the SELECTs, you did nothing to restrict the rows to those books published in boston
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-27-10, 19:05
harofreak00 harofreak00 is offline
Registered User
 
Join Date: Apr 2010
Posts: 3
whoa, whoops.

Round 2:

Code:
SELECT B.BookCode, B.Title, price
FROM Book B
WHERE B.Price > '10'
UNION
SELECT B.BookCode, B.Title, price
FROM Book B, Publisher P
WHERE P.City = 'Boston'
AND B.PublisherCode = P.PublisherCode;
However, this code only displays books with prices higher than 10, it doesn't include the Boston books.
Reply With Quote
  #4 (permalink)  
Old 04-27-10, 19:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by harofreak00 View Post
... it doesn't include the Boston books.
oh yes it does

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 04-27-10, 19:23
harofreak00 harofreak00 is offline
Registered User
 
Join Date: Apr 2010
Posts: 3
I guess its kind of a stupid question, because after further looking, ALL of the Boston books (only 2 of them), are both more then $10. I guess I have found the solution! Thanks for the help.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On