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 7547897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:34:21+00:00 2026-05-30T09:34:21+00:00

I am working on a project right now and am rather stumped with a

  • 0

I am working on a project right now and am rather stumped with a specific sql query I (need) to execute. Let me start off by showing the DB structure I need to pull from.

    --posts_table--
    ID
    post_title
    post_text
    bus_id

This next table is what is screwing with me. The only way data related to the logged in user is in here is if they have “liked” a specific post — otherwise there is no data related to that user in this table. Now there could be plenty of data related to a particular post, just generated from other users.

    --likes_table--
    ID
    user_id
    post_id
    like

What I need this to do is grab all the posts from the post_table above where a specific business id is specified. From there, I need it to grab the “like” column in the likes_table if there is data in there related to the logged in user. If there is no data there, just leave that field null in the query. Below is a query I wrote that works until there is other “like” data in the like_table from other users.

    SELECT posts.id, posts.post_text, posts.post_title, likes.post_id, likes.like
        FROM posts LEFT JOIN likes ON posts.id = likes.post_id WHERE
        posts.bus_id = 1 AND likes.user_id IS NULL OR likes.user_id = 1;

This works up until data has been entered in the table about a specific post being liked by a different user before that user has done anything with that post, whether they like or dislike it. I am not sure if this specific type of query is even possible, any help would be much appreciated.

Edit:

After looking at it again — I got it, finally. I just needed to add one more AND. Below is the proper query I was looking for.

    SELECT posts.id, posts.post_text, posts.post_title, likes.post_id, likes.like
    FROM posts LEFT JOIN likes ON posts.id = likes.post_id AND posts.user_id = 1 WHERE
    posts.bus_id = 1 AND likes.user_id IS NULL OR likes.user_id = 1;
  • 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-30T09:34:23+00:00Added an answer on May 30, 2026 at 9:34 am

    Ahh, I think I get you — is it that if a particular post hasn’t been commented on by user_id number 1 at all, the row for that doesn’t show up at all?

    In that case, put your l.user_id=1 into the JOIN condition instead of the WHERE condition — this will put a NULL in if user_id 1 hasn’t liked or disliked a particular post.

    SELECT p.id, p.post_text, p.post_title, l.post_id, l.likes
      FROM posts p
      LEFT JOIN likes l ON p.id = l.post_id AND l.user_id=1
      WHERE p.bus_id = 1 
    

    The l.user_id IS NULL OR l.user_id=1 has been incorporated into the LEFT JOIN — it doesn’t make rows for the other user_ids.

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

Sidebar

Related Questions

I'm working on a project right now in which I need to automate animated
I am working on a project right now that involves receiving a message from
I'm working on a Flash project right now and I am having to force
Right now a project I'm working on has reached a level of complexity that
On this project I am working on right now, one of the newest feature
Right now I am working on a stub of a project. In the course
In the .net 3.5 project that I am working on right now, I was
I'm working on a project right now that required me to use a CMS
I'm working on a project right now where I have been slowly accumulating a
I am working on a project right now that has to separate project files.

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.