Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2012
    Posts
    2

    Unanswered: Querying 4 tables w/Export to Excel

    Hi, I'm wondering could anyone help me here please.

    In my database there's 4 tables that I need to query from, and each one has a common field called reviewId. My query at the moment is below, its not the correct one I no but it takes the information and prints it to an Excel 4 times instead of once.

    Code:
    $query = "SELECT * FROM review, mom, remark, action
    WHERE review.reviewId IN( ".$_POST['reviewIds']." )";
    reviewIds from above is used to take in whatever ID the user enters. So for example if the user enters ID 12 it will take everything associated with ID 12 from all 4 tables and print it to an Excel file.

    My 4 tables are: review, remark, action & mom. I need to take all of the information from each 4 tables that is associated with whatever ID the user enters.

    I have a query that queries two tables and works successfully but I don't no how to do it through querying 4 tables. The query is below -

    Code:
    $query = "SELECT review.*, mom.result, mom.date FROM review 
    LEFT JOIN mom ON review.reviewId = mom.reviewId WHERE
    review.reviewId IN ( ".$_POST['reviewIds']." )";

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    do it the same way -- with JOIN and ON
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Jun 2012
    Posts
    2
    Quote Originally Posted by r937 View Post
    do it the same way -- with JOIN and ON
    Like this?

    Code:
    SELECT review . * , mom . * , 
    ACTION . * , remark . * 
    FROM review
    LEFT JOIN mom ON review.reviewId = mom.reviewId
    LEFT JOIN remark ON review.reviewId = remark.reviewId
    LEFT JOIN ACTION ON review.reviewID = action.reviewId
    WHERE review.reviewId =  '12'

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    I dunno
    why not try it and see what happens
    If it doesnt' work then try to work out what isn't working
    I'd rather be riding on the Tiger 800 or the Norton

Tags for this Thread

Posting Permissions

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