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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:19:26+00:00 2026-05-25T03:19:26+00:00

I have been altering a query for a while now, reading many posts on

  • 0

I have been altering a query for a while now, reading many posts on this wonderful site to get to where I am with this so far. But, alas, now I am stuck 🙁

This query and relevant part of the database I have designed is similar to the youtube comment liking system. Relevant tables and fields are:

USRS
usr_id int (PK)

COMMENTS
comment_id int (PK)
usr_id int (FK) references usrs(usr_id)
topic_id int (FK) references topics(topic_id)
descr varchar
created varchar

COMMENT_LIKERS
comment_id int (PK) (FK) references comments(comment_id)
usr_id int (PK) (FK) references usrs(usr_id)
liker tinyint

I want to be able to select all relevant data in one query. Aside from general data for each comment, I want to count all likes and dislikes for each comment. The query I have thus far is counting the likes for ALL comments and not for each even though I have the LEFT JOIN with ON clause: comments.comment_id = comment_likers.comment_id.

I am learning MySql and PHP so go easy with me if I have done something silly. I assure you I have looked all around for clues to the answer.

Here is the query:

SELECT comments.comment_id, comments.descr, comments.created, usrs.usr_name,
  COUNT(if(comment_likers.liker = 1, 1, null)),
  COUNT(if(comment_likers.liker = 0, 1, null)),
  comment_likers2.liker
FROM comments
INNER JOIN usrs ON ( comments.usr_id = usrs.usr_id )
LEFT JOIN comment_likers ON ( comments.comment_id = comment_likers.comment_id )
LEFT JOIN comment_likers AS comment_likers2 ON ( comments.comment_id = comment_likers.comment_id AND comment_likers.usr_id = $usrID )
WHERE comments.topic_id = $tpcID
GROUP BY comments.comment_id
ORDER BY comments.created DESC

Thanks in advance

  • 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-25T03:19:27+00:00Added an answer on May 25, 2026 at 3:19 am
    SELECT comments.comment_id, comments.descr, comments.created, usrs.usr_name, 
      (SELECT COUNT(*) FROM comment_likers WHERE comment_id=comments.comment_id AND liker=1)likes,
      (SELECT COUNT(*) FROM comment_likers WHERE comment_id=comments.comment_id AND liker=0)dislikes
      liker
    FROM comments
    INNER JOIN usrs ON ( comments.usr_id = usrs.usr_id )
    LEFT JOIN comment_likers  ON ( comments.comment_id = comment_likers.comment_id 
     AND comment_likers.usr_id = $usrID )
    WHERE comments.topic_id=$tpcID
    ORDER BY comments.created DESC;
    

    A couple notes. I wasn’t too sure what the second left join on comment_likers was supposed to accomplish (the one using $usrID). Are you only interested in likes from on a specific topic from a specific user?

    Also, you might think about changing the schema for comments created to be a datetime instead of a varchar.

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

Sidebar

Related Questions

Have been googling for a while, but I don't find any page which explains
Have been scatching my head about this - and I reckon it's simple but
Have been wondering about this for days now: I have a basic wxpython program
Have been working in Ruby for a while, but usually in the context of
Have been studying the file system related classes of Adobe AIR 1.5, but so
Have been struggling with Javascript closure for a while trying to wrap brain around
We have been using CruiseControl for quite a while with NUnit and NAnt. For
I realize there have been a few other questions on this topic, and the
Have been reading around on IErrorHandler and want to go the config route. so,
Good evening, I am tired and have been fighting with this for hours. I

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.