SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Joining results of 2 select queries

    Hi guys I need your help!

    I have a select query that joins 2 table. What I would like to happen is that the result of that query (2 join table) will be join to another select query result.

    example (2 join table)
    | x | y |
    | 1 | x |
    | 2 | y |

    to be join on another select query

    | w | q |
    | 1 | R |
    | 3 | E |

    result would be

    | x | y | q |
    | 1 | x | R |
    | 2 | y | Null |

    How could I possibly do this?

    Thanks in advance

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    How do you end up with "x | y | q"?? What's the join condition that linked those two rows?

    You can join as many tables as you need, not just 2. Join a 3rd, or a 4th, or a 5th!

  3. #3
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    How do you end up with "x | y | q"?? What's the join condition that linked those two rows?

    You can join as many tables as you need, not just 2. Join a 3rd, or a 4th, or a 5th!
    "x | y | q" will be the result if x would match w. I hope I was able to clear it out.

  4. #4
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by comwwwdot View Post
    "x | y | q" will be the result if x would match w. I hope I was able to clear it out.
    Table 1: A result of my first query(having left joins in it)

    Table 2: A result of my second query


    I want to join the 2 tables from 2 different queries

    the two tables will be joined matching the tables stuID. A null is returned on the results of table 2 if no match is found.

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,340
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    could you actually show your two queries please
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    could you actually show your two queries please
    I think I was able to get what I wanted using this query

    Select resScore, resTotal, y3.stuID, stuCourse, stuYearLevel, stuSection, accFName, accLName from (select * from result where result.exaID = '$exaID') as y2 right join (SELECT student.stuID, stuCourse, stuYearLevel, stuSection, accFName, accLName FROM classlist LEFT JOIN account ON classlist.accID = account.accID LEFT JOIN student ON student.accID = account.accID INNER JOIN class ON classlist.claID = class.claID WHERE classlist.claID = '$class' AND class.insID = '$user') AS y3 on y2.stuID = y3.stuID order by accFName asc

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,340
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    you could probably just have extended the joins in your second query

    and your LEFT OUTER JOINs should probably be INNER JOINs
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  8. #8
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by r937 View Post
    you could probably just have extended the joins in your second query

    and your LEFT OUTER JOINs should probably be INNER JOINs
    Thank you so much sir! I have just fixed my query . Thanks again


Tags for this Thread

Bookmarks

Posting Permissions

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