SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SQL Query help to get topics and topics comments

    Just need some guidance on the best way to make a query on the database.
    I am building a site that has your status updates feature and comments for that status update.
    Just not sure how to write the sql to pull it all out. I am running a query to get all the status updates assumed I would need to run another query while looping / printing out each status to get all the comments, but there must be another more efficient way.

    I have two tables, one for statuses and the other for comments with the status id as key in comments. This is what I was thinking of doing but it looks really inefficient having to run two queries on the database. Any help appreciated.


    select * from status

    foreach statuses as status

    print status

    select * from comments where status_id = status_id

    foreach comments as comment

    print comment

    end comments

    end status

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,998
    Mentioned
    100 Post(s)
    Tagged
    0 Thread(s)
    What is the structure of each table?

    What fields (and from which table) are required in the result set?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    What is the structure of each table?

    What fields (and from which table) are required in the result set?

    Table user

    userid, username,


    Table status

    statusid, userid, status, date,

    Table comments

    commentid, userid, statusid, date, comment


    So i basically want to list all the statuses and underneith each status all the comments on that status. As in my previous post, I can do this using nested for loops and queries, but is there any way I can just run one query and pull out all the data at once?

    Thanks.


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
  •