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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:09:30+00:00 2026-05-18T01:09:30+00:00

Consider this classical setup: entry table: id (int, PK) title (varchar 255) entry_category table:

  • 0

Consider this classical setup:

entry table:
id (int, PK)
title (varchar 255)

entry_category table:
entry_id (int)
category_id (int)

category table:
id (int, PK)
title (varchar 255)

Which basically means entries can be in one or more categories (the entry_category table is used as MM/join table)

Now I need to query 6 unique categorys along with 1 unique entries from these categories by RANDOM!

EDIT: To clarify: the purpose of this is to display 6 random categories with 1 random entry per category.

A correct result set would look like this:

category_id   entry_id     
10            200 
20            300
30            400
40            500
50            600
60            700

This would be incorrect as there are duplicates in the category_id column:

category_id  entry_id
10           300
20           300
...

And this is incorrect as there are duplicates in the member_id column:

category_id  entry_id     
20           300
20           400
...

How can I query this?

If I use this simple query with order by rand, the result contains duplicated rows:

select c.id, e.id
from category c    
inner join entry_category ec on ec.category_id = c.id
inner join entry e on e.id = ec.entry_id
group by c.id
order by rand()

Performance is at the moment not the most important factor, but I would need a reliably working query for this, and the above is pretty much useless and does not do what I want at all.

EDIT: as an aside, the above query is no better when using select distinct ... and leaving out the group by. This includes duplicate rows as distinct only makes sure that the combinations of c.id and e.id are unique.


EDIT: one solution I found, but probably slow as hell on larger datasets:

select t1.e_id, t2.c_id
from (select e.id as e_id from entry e order by rand()) t1
inner join (select ec.entry_id as e_id, ec.category_id as c_id from entry_category ec group by e_id order by rand()) t2 on t2.e_id = t1.e_id
group by t2.c_id
order by rand()
  • 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-18T01:09:31+00:00Added an answer on May 18, 2026 at 1:09 am
    SELECT  category_id, entity_id
    FROM    (
            SELECT  category_id,
                    @ce :=
                    (
                    SELECT  entity_id
                    FROM    category_entity cei
                    WHERE   cei.category_id = ced.category_id
                            AND NOT FIND_IN_SET(entity_id, @r)
                    ORDER BY
                            RAND()
                    LIMIT 1
                    ) AS entity_id,
                    (
                    SELECT  @r := CAST(CONCAT_WS(',', @r, @ce) AS CHAR)
                    )
            FROM    (
                    SELECT  @r := ''
                    ) vars,
                    (
                    SELECT  DISTINCT category_id
                    FROM    category_entity
                    ORDER BY
                            RAND()
                    LIMIT 15
                    ) ced
           ) q
    WHERE  entity_id IS NOT NULL
    LIMIT  6
    

    This solution is not a piece of code I’d be proud of, since it relies on black magic of session variables in MySQL to keep the recursion stack. However, it works.

    Also it’s not perfectly random and can in fact yield less than 6 values (if entity_id‘s duplicate across the categories too often). In this case, you can increase the value of 15 in the innermost query.

    Create a unique index or a PRIMARY KEY on category_entity (category_id, entity_id) for this to work fast.

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

Sidebar

Related Questions

Consider this code: template <int N> struct X { friend void f(X *) {}
Consider this scenario we have a class A which is singleton it has a
Consider this as my json string, {Table : [{userid : 11,name : KumarP,designation :
First, let me ask you to consider this as a real question, and not
Consider this: One mySQL database that has tables and rows and data within it.
consider this string prison break: proof of innocence (2006) {abduction (#1.10)} i just want
Consider this macro: #define MAKE_TEMPLATE(...) template <typename T, __VA_ARGS__ > When used with zero
consider this piece of code: Welcome to Scala version 2.8.0.r0-b20100714201327 (Java HotSpot(TM) 64-Bit Server
Consider this example SOAP Client script: $SOAP = new SoapClient($WDSL); // Create a SOAP
Consider this function signature: Private Void TextBox1_TextChange(Object Sender, EventArgs e) As far as my

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.