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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:08:09+00:00 2026-05-14T23:08:09+00:00

I have two tables, rating: +———–+———–+————-+———-+ | rating_id | entity_id | rating_code | position

  • 0

I have two tables, rating:

+-----------+-----------+-------------+----------+
| rating_id | entity_id | rating_code | position |
+-----------+-----------+-------------+----------+
|         1 |         1 | Quality     |        0 |
|         2 |         1 | Value       |        0 |
|         3 |         1 | Price       |        0 |
+-----------+-----------+-------------+----------+

And rating_option

+-----------+-----------+------+-------+----------+
| option_id | rating_id | code | value | position |
+-----------+-----------+------+-------+----------+
|         1 |         1 | 1    |     1 |        1 |
|         2 |         1 | 2    |     2 |        2 |
|         3 |         1 | 3    |     3 |        3 |
|         4 |         1 | 4    |     4 |        4 |
|         5 |         1 | 5    |     5 |        5 |
|         6 |         2 | 1    |     1 |        1 |
|         7 |         2 | 2    |     2 |        2 |
|         8 |         2 | 3    |     3 |        3 |
|         9 |         2 | 4    |     4 |        4 |
|        10 |         2 | 5    |     5 |        5 |
|        11 |         3 | 1    |     1 |        1 |
|        12 |         3 | 2    |     2 |        2 |
|        13 |         3 | 3    |     3 |        3 |
|        14 |         3 | 4    |     4 |        4 |
|        15 |         3 | 5    |     5 |        5 |
+-----------+-----------+------+-------+----------+

I need a SQL query (not application level, must stay in the database) which will select a set of ratings randomly. A sample result would look like this, but would pick a random value for each rating_id on subsequent calls:

+-----------+-----------+------+-------+----------+
| option_id | rating_id | code | value | position |
+-----------+-----------+------+-------+----------+
|         1 |         1 | 1    |     1 |        1 |
|         8 |         2 | 3    |     3 |        3 |
|        15 |         3 | 5    |     5 |        5 |
+-----------+-----------+------+-------+----------+

I’m totally stuck on the random part, and grouping by rating_id has been a crap shoot so far. Any MySQL ninjas want to take a stab?

Thanks,
Joe


EDIT: I’ve tried rand() in a bunch of combinations, and I’m sure that it will be necessary to create the randomness of the result, but I cannot figure out how to return one random row for each of the rows in rating. I cannot use order by rand() limit 1 because I need three rows, and order by rand() limit 3 won’t give me one of each rating_id, which is the ultimate goal. I need a combination of rand() and either subqueries or joins so that I can ensure one of each rating_id.

  • 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-14T23:08:10+00:00Added an answer on May 14, 2026 at 11:08 pm

    Alright, a little messy, but seems to do the job. Someone may know what they’re doing better than I do that can clean this up:

    SELECT random.rating_id, random.rand_option_id, r3.code, r3.value, r3.position
    FROM
        (SELECT r.rating_id,
            (SELECT r2.option_id
             FROM rating_option r2
             WHERE r2.rating_id = r.rating_id
             ORDER BY RAND() LIMIT 1) AS 'rand_option_id'
         FROM rating_option r
         GROUP BY r.rating_id
        ) random
        LEFT JOIN rating_option AS r3 ON r3.option_id = rand_option_id
    

    Results (varies every time, of course):

    +-----------+----------------+------+-------+----------+
    | rating_id | rand_option_id | code | value | position |
    +-----------+----------------+------+-------+----------+
    |         1 |              4 |    4 |     4 |        4 |
    |         2 |              6 |    1 |     1 |        1 |
    |         3 |             13 |    3 |     3 |        3 |
    +-----------+----------------+------+-------+----------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 439k
  • Answers 439k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The way I do it is to: get all Git… May 15, 2026 at 4:57 pm
  • Editorial Team
    Editorial Team added an answer There is no defined order for attribute nodes according to… May 15, 2026 at 4:57 pm
  • Editorial Team
    Editorial Team added an answer You can use ElementName to set the binding source to… May 15, 2026 at 4:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.