Results 1 to 2 of 2
  1. #1
    Join Date
    May 2007
    Posts
    3

    Unanswered: 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?

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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'
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •