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 > Not sure what sort of query to use - JOIN?.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-17-10, 09:52
lou73 lou73 is offline
Registered User
 
Join Date: Jun 2010
Posts: 2
Not sure what sort of query to use - JOIN?.

I have 3 tables Jobs, Quotes and Questions eg:-

Jobs
-----------
JobID
JobName

Quotes
-----------
JobID
UserID

Questions
-----------
JobID
UserID

I'm trying to get a grouped result set of JobID's where UserID = ? in either Quotes or Questions I don't think I can use a JOIN as the user may have entered questions but not a quote.

Nearest thing I have so far is this but the grouping obvously won't work, I'm also not familiar with UNION and think I might be going down the wrong path.

PHP Code:
SELECT
  JobID
UserID
FROM Quotes
UNION ALL
SELECT
 JobID
UserID
FROM Questions
WHERE UserID 
= ? 
Reply With Quote
  #2 (permalink)  
Old 06-17-10, 10:26
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by lou73 View Post
I'm trying to get a grouped result set of JobID's
not sure what you mean by a "grouped" result

but your UNION query is fine (you should change UNION ALL to UNION, though)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-17-10, 10:51
lou73 lou73 is offline
Registered User
 
Join Date: Jun 2010
Posts: 2
Quote:
Originally Posted by r937 View Post
not sure what you mean by a "grouped" result

but your UNION query is fine (you should change UNION ALL to UNION, though)
Thanks for your help.

Basically I only wanted the JobID retuned once whether it's in either table. I was thinking I needed to use the GROUP BY clause or a different approach, but I've got it working 'I think' usining your suggestion and adding in another WHERE clause as follows:-

PHP Code:
SELECT 
  JobID
UserID 
FROM Quotes 
WHERE UserID 
= ?
UNION 
SELECT 
 JobID
UserID 
FROM Questions 
WHERE UserID 
= ? 
Reply With Quote
  #4 (permalink)  
Old 06-17-10, 10:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
beauty!!

you fixed an error that i had completely overlooked when i said it was fine

you arrived at the correct solution despite my assistance

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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