SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    The Netherlands
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert query in loop to single query

    Hello,

    Let's say I'm displaying posts using a query (disregarding normalization) such as

    Code:
    SELECT * FROM posts WHERE foreign_id = x
    Now, for every post I want to include what that user has on their Currently Watching list, up to 3 maximum.

    So for every post I'm currently doing a
    Code:
    SELECT * FROM currently_watching WHERE user = x LIMIT 3
    (if it were just one, i could've simply joined the table in the first query)
    Could this be rewritten into a single query, preferably where the resulting rows would look something like

    User | Message | Watching1 | Watching2 | Watching3

    Thanks in advance,

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,016
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Sheqel View Post
    Could this be rewritten into a single query, preferably where the resulting rows would look something like
    not with SQL

    you could put the LIMIT into a subquery, but without ORDER BY, i'm wondering why you would want to show any old three
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    The Netherlands
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't add the order by clause because I didn't think it was relevant to the problem


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
  •