SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Location
    Boston
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help with query to find posts with no comments

    I have a very simple relationship between posts and comments:

    posts(id, title, body)
    comments(id, post_id, body)

    Is it possible to write a query that finds all posts that have no comments attached?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,276
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    yes!!
    Code:
    SELECT p.id
         , p.title
      FROM posts AS p
    LEFT OUTER
      JOIN comments AS c
        ON c.post_id = p.id
     WHERE c.post_id IS NULL
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Location
    Boston
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks! that worked beautifully.


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
  •