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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:38:54+00:00 2026-06-06T11:38:54+00:00

I have a query that is pulling users who liked a specific object from

  • 0

I have a query that is pulling users who liked a specific object from a users table. Ratings are stored in a ratings table. The query I have come up with so far looks like this:

SELECT user.id, user.name, user.image
  FROM users
LEFT JOIN ratings ON ratings.userid = user.id
WHERE rating.rating > 0
  AND rating.objectId IN (1,2,3,4)

I want to be able to put a LIMIT on this query, to avoid returning all the results, when I only need 3 or so results for each ID. If I just put a LIMIT 12 for example, I might get 8 records with one id, and 1 or 2 each for the others – i.e. an uneven distribution across the IDs.

Is there a way to write this query so as to guarantee that (assuming an object has been “liked” at least three times), I get three results for each of the ids in the list?

  • 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-06T11:38:56+00:00Added an answer on June 6, 2026 at 11:38 am

    By setting the row number whit variables, and then filter that result to show only row 1-3 should work

    SET @last_objectId = 0;
    SET @count_objectId = 0;
    SELECT id, name, image FROM (
    SELECT
     user.id,
     user.name,
     user.image,
     @count_objectId := IF(@last_objectId = rating.objectId, @count_objectId, 0) + 1 AS rating_row_number,
     @last_objectId := rating.objectId
    FROM users
    LEFT JOIN ratings ON (ratings.userid = user.id)
    WHERE
     rating.rating > 0 AND
     rating.objectId IN (1,2,3,4)
    ORDER BY rating.objectId
    ) AS subquery WHERE rating_row_number <= 3;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a somewhat complex SQL query that is pulling from a table of
I have an INSERT query that is pulling data from two tables and inserting
I have a query that successfully grabs the unique products from my products table
I have a MS SQL Query that is pulling data via from a remote
I have a query that basically combines tables of actions and selects from them
I have a query that looks like this SELECT * from myTable WHERE Date
I have a query and a loop written that lists all the rows from
I have a query that is meant to return user details from Wordpress tables.
I have a custom query in WordPress that is pulling a time assigned to
I have a query pulling from a database: List<myClass> items = new List<myClass>(from 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.