Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6010899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:10:56+00:00 2026-05-23T02:10:56+00:00

With the following tables posts id post_id user_id comments id post_id comment_id user_id deleted

  • 0

With the following tables

posts

  • id
  • post_id
  • user_id

comments

  • id
  • post_id
  • comment_id
  • user_id
  • deleted

replies

  • id
  • post_id
  • reply_id
  • user_id
  • deleted

I am trying to get every comment and reply from each post.post_id and post.user_id=’x’ and the comment or reply is not deleted(0)

this is what i have tried

SELECT *
FROM posts p
LEFT JOIN comments c ON p.id=c.post_id
LEFT JOIN replies r ON p.id=r.post_id
WHERE p.user_id=$user_id
&& c.deleted='0' && r.deleted='0'

which does not work…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:10:57+00:00Added an answer on May 23, 2026 at 2:10 am

    A post may have comments or not. Use LEFT JOIN instead of INNER JOIN.

    A post may have replies or not. Use LEFT JOIN instead of INNER JOIN in that join too.

    When LEFT JOIN is used, a condition like WHERE comments.deleted = 0 that includes a field (from the right table (comments) in the LEFT JOIN), the LEFT JOIN is cancelled. So, we should put this condition in the ON clause and not in the WHERE.

    SELECT *
    FROM posts p
      LEFT JOIN comments c
        ON p.post_id = c.post_id
        AND c.deleted = 0
      LEFT JOIN replies r
        ON p.post_id = r.post_id
        AND r.deleted = 0  
    WHERE p.user_id = $user_id
    

    Thinking more clearly, the above will show what the question describes but in cases with say, 4 comments and 3 replies, 12 rows will be returned (3×4). Which is probably not wanted. The following 2nd try does not have such issue.

    I don’t see a post.text or comment.text or reply.text in the tables but anyway, you’ll get the idea. You can remove the 3 text lines if not appropriate.

      ( SELECT p.post_id     AS post_id 
             , 0             AS type
             , p.post_id     AS id
             , p.text        AS text
        FROM posts p
        WHERE p.user_id = $user_id
      )
    UNION ALL
      ( SELECT p.post_id     AS post_id 
             , 1             AS type
             , c.comment_id  AS id
             , c.text        AS text
        FROM posts p
          JOIN comments c
            ON p.post_id = c.post_id
        WHERE p.user_id = $user_id
          AND c.deleted = 0
      )
    UNION ALL
      ( SELECT p.post_id     AS post_id 
             , 2             AS type
             , r.reply_id    AS id
             , r.text        AS text
        FROM posts p
          JOIN replies r
            ON p.post_id = r.post_id
        WHERE p.user_id = $user_id
          AND r.deleted = 0
      )
    ORDER BY post_id
           , post_type
           , id
    

    The 0,1,2 stand for post, comment, reply.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables: posts (post_id, content, etc) comments (comment_id, post_id, content, etc)
I have following tables: users(id, name) posts (id, text, userId) comments (id, text, userId,
Problem Given the following two tables, I'd like to select all Ids for Posts
The following query selects all posts and each post's owner, all of the comments
I have tables for a forum system. I am tring to get the following
I have the following tables in my database that have a many-to-many relationship, which
Imagine the following tables: create table boxes( id int, name text, ...); create table
I have the following tables in MySQL: team: id, name, [more stuff] person: id,
Given the following tables in ActiveRecord: authors sites articles I don't know how to
I have the following tables: 1-Categories: -CategoryID -CategoryName -ParentID 2-Items: -ItemId -ItemName -CategoryID categories

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.