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

  • Home
  • SEARCH
  • 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 7774769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:27:37+00:00 2026-06-01T17:27:37+00:00

Not sure if my title explains it very well so here’s an example. I

  • 0

Not sure if my title explains it very well so here’s an example. I have two MYSQL tables, comments and votes_comments which are matched based on a field comment_id. For sample data comments has two rows and votes_comments has one row which matches one of the comments row. AKA Two comments were written and a single person voted on one of them Given that, the following query returns 2 rows:

SELECT comments.*, users.username FROM comments 
JOIN users ON users.user_id = comments.user_id 
WHERE comments.track_id=6 AND comments.parent_id=-1
GROUP BY comments.comment_id

Now what I want to do is join on votes_comments which may or may not have a matching row. What I want to do is grab the matching value if it exists or return null in the column if there is no matching result. AKA: Did the user vote it up, down or not at all (null). Here is my modified query:

SELECT comments.*, users.username, votes_comments.vote FROM comments 
JOIN users ON users.user_id = comments.user_id 
LEFT JOIN votes_comments ON votes_comments.comment_id = comments.comment_id
WHERE track_id=6 AND parent_id=-1 AND votes_comments.user_id=1
GROUP BY comments.comment_id

This query only returns one row since I only have a single row in votes_comments. I thought that left join would return results the way I want but apparently not. Any ideas how to get what I’m looking for?

  • 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-06-01T17:27:38+00:00Added an answer on June 1, 2026 at 5:27 pm

    I suspect the WHERE clause AND votes_comments.user_id=1 is causing the problem.

    Read a tutorial on Joins and you’ll see that the result of your LEFT JOIN is a temporary table with two rows, and for each row, fields from comments JOIN users and from votes_comments. If no matching record from votes_comments exists, the fields in the temporary table are filled with NULL values.

    Your WHERE clause, AND votes_comments.user_id=1, will fail because votes_comments.user_id is NULL in the rows of user with no matching votes_comments record.

    If what you actually want is to join to the row in votes_comments what matches a specific user ID (e.g. ID=1), then your ON clause should be something like

    ON votes_comments.comment_id = comments.comment_id
        AND votes_comments.user_id=1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know if the title explains it very well, so here's what I'm
Not sure the title explains what I want to do, so here goes: I
I'm not sure the title explains it well enough, but was trying to keep
Not sure if the title covers the question well, but here it comes. I
I am not sure my title is correct or not since I have no
As the post title implies, I have a legacy database (not sure if that
Sorry for the not very descriptive question title. I'm not very sure how to
Not too sure if the title to this actually explains what I really need
I have two requirements, and im not sure is those things possible in AS3.
I am not sure whether the title clearly explains my problem, will try to

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.