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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:52:15+00:00 2026-05-18T08:52:15+00:00

Following on from this question last_question with this table `id`, `bbs_id`, `user_id`, `like_dislike` (15,

  • 0

Following on from this question
last_question with this table

`id`, `bbs_id`, `user_id`, `like_dislike`
(15,   4,        2,         0),
(14,   4,        1,         0),
(13,   3,        1,         0),
(12,   2,        1,         1),
(11,   1,        2,         0),
(10,   1,        1,         1);

How can I see what an individual users like or dislike was? Lets say I wanted to have an aggregate table of all the likes and dislikes with another column for whether user x liked it.

This is the query that I have tried

$user_id = 1;


SELECT bbs_id,
     (SELECT like_dislike FROM bb_ratings WHERE user_id={$user_id}) AS thisUsersRating,
       SUM(CASE WHEN like_dislike = 1 THEN 1 ELSE 0 END) AS likes, 
       SUM(CASE WHEN like_dislike = 0 THEN 1 ELSE 0 END) AS dislikes
FROM bb_ratings
GROUP BY bbs_id

I guess the problem I am running into here is, how do you refer to user_id = x in this particular row, not in all the rows.
Thanks in Advance
Andrew

  • 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-18T08:52:15+00:00Added an answer on May 18, 2026 at 8:52 am

    I have added an answer to the previous question, please refer to that first, for understanding.

    You can’t get it from bb_ratings alone by GROUPing it and hacking it. You get Null because you are thinking in terms of a grid, not relational sets (that is the central concept of the Relational Model).

    1. Before you decide which table(s) to go to, to service your query, you need to decide what you want for the structure of your result set.

    2. Then constraint it (which rows) with the WHERE clause.

    3. Then figure out where (what tables) to get the columns from. Either joins to more tables, and more work on the WHERE clause; or scalar subqueries, correlated to the outer query.

    You are not clear about what you want. It looks like you want the same report as the previous question, plus a column for the given users vote. To me, the structure of your result set is a list of bulletins. Well, I can get that from bulletin, no need to go to bulletin_like and then have to GROUP that.

    If you think in terms of sets, it is very easy, no need to jump through hoops with materialised views or “nested” queries:

    SELECT name AS bulletin, 
        (SELECT COUNT(like) 
            FROM  bulletin_like bl 
            WHERE bl.bulletin_id = b.bulletin_id 
            AND   like = 1
            ) AS like,
        (SELECT COUNT(like) 
            FROM  bulletin_like bl 
            WHERE bl.bulletin_id = b.bulletin_id 
            AND   like = 0
            ) AS dislike,
        (SELECT COUNT(like) 
            FROM  bulletin_like bl 
            WHERE bl.bulletin_id = b.bulletin_id 
            AND   bl.user_id = {$user_d}
            AND   like = 1
            ) AS your_vote
        FROM bulletin b

    Responses to Comments

    I have the feeling that what you are saying is very important for how I approach SQL

    1. Yes, absolutely. If you are willing to learn the right stuff up front, it will:

      • save you all kinds of problems later
      • make your queries more efficient
      • allow you to code faster
        .
    2. For now, forget about using result sets as tables (much slower), and temp tables (definitely not required if your database is Normalised). You are much better off querying the tables directly. You need to learn various subjects such as the Relational model; how to use SQL; how not to use SQL so as to avoid problems; etc. I am willing to help you, and stay with you for a while, but I need to know you are willing. It will take a bit of back-and-forth. There is a bit of noise on this site, so I will ignore other comments (until the end) and respond only to yours.

      • stop using GROUP BY, it is seriously hindering your understanding of SQL. If you can't get the report you want without using GROUP BY, ask a question.
        .
    3. This posted question. Let me know at which point you got lost, and I will provide more detail from that point forward.

      • For this question, you want a list of bulletins, with likes; dislikes; and this users likes. Is that correct ? Have you tried the code I provided ?
        .
    4. Looked at the linked question. It is a mess, and no one has addressed the deeper problem; they have answered the problem on the surface, in isolation. You now have an answer but you do not understand it. That is a very slow way to progress.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from this question , I am interested in finding out how you
following on from this question (Developing to an interface with TDD), I'm still having
Following on from this question...I'm trying to unit test the following scenario: I have
Following on from this question... What to do when you’ve really screwed up the
Following the CSS style trick from this question I was able to create a
Following this question, it seems that it is possible to open a file from
Okay, this is following on from my previous question reguarding performing a simple ajax
Check this example before reading the question - http://www.sqlfiddle.com/#!2/fcf3e/8 The following data comes from
This problem follows on from a previous question . When I run the following
I am hoping that somebody can help me out with this question. The following

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.