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 > Joining rows/columns...is it subquery?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-31-07, 00:12
icognito01 icognito01 is offline
Registered User
 
Join Date: May 2007
Posts: 3
Joining rows/columns...is it subquery?

I'm joining two tables (questions and choices) to get a result like this based on a ID called 'Q007'
SELECT c.code, c.label
FROM questions q, choices c
WHERE q.id = c.id
AND q.id = 'Q007'

Code Label
1 1
2 5
3 10
4 15
5 20
6 25
7 30

Now, I have a 'questiondata' table which have an 'id' as a column name. The rows contain the 'code' based on the 'choices' table.

SELECT qd.Q007
FROM questionsdata dq

Q007
1
2
6
1
2
6
6
1
5
7
2

Now, I want to list sometihng like, (Q007, label) that contains the two queries shown above. How would I do that? Should I use a subquery? Example, I want my output to look like:

Q007 label
1 1
2 5
6 25
1 1
2 5
6 25
6 25
1 1
5 20
7 30


Any ideas?
Reply With Quote
  #2 (permalink)  
Old 05-31-07, 02:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT qd.Q007
     , c.label
  FROM questions q
INNER
  JOIN choices c
    ON c.id = q.id
INNER
  JOIN questionsdata dq
    ON d1.Q07 = c.code
 WHERE q.id = 'Q007'
__________________
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