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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:08:09+00:00 2026-06-08T14:08:09+00:00

Thanks for looking, I’m trying to get 20 entries from the database randomly and

  • 0

Thanks for looking, I’m trying to get 20 entries from the database randomly and unique, so the same one doesn’t appear twice. But I also have a questionGroup field, which should also not appear twice. I want to make that field distinct, but then get the ID of the field selected.

Below is my NOT WORKING script, because it does the ID as distinct too which

SELECT DISTINCT `questionGroup`,`id` 
FROM `questions` 
WHERE `area`='1' 
ORDER BY rand() LIMIT 20

Any advise is greatly appreciated!

Thanks

  • 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-08T14:08:12+00:00Added an answer on June 8, 2026 at 2:08 pm

    Try doing the group by/distinct first in a subquery:

    select *
    from (select distinct `questionGroup`,`id`
          from `questions`
          where `area`='1'
         ) qc
    order by rand() 
    limit 20
    

    I see . . . What you want is to select a random row from each group, and then limit it to 20 groups. This is a harder problem. I’m not sure if you can do this accurately with a single query in mysql, not using variables or outside tables.

    Here is an approximation:

    select *
    from (select `questionGroup`
                 coalesce(max(case when rand()*num < 1 then id end), min(id)) as id
          from `questions` q join
                (select questionGroup, count(*) as num
                 from questions
                 group by questionGroup
                ) qg
                on qg.questionGroup = q.questionGroup
          where `area`='1'
          group by questionGroup
         ) qc
    order by rand() 
    limit 20
    

    This uses rand() to select an id, taking, on average two per grouping (but it is random, so sometimes 0, 1, 2, etc.). It chooses the max() of these. If none appear, then it takes the minimum.

    This will be slightly biased away from the maximum id (or minimum, if you switch the min’s and max’s in the equation). For most applications, I’m not sure that this bias would make a big difference. In other databases that support ranking functions, you can solve the problem directly.

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

Sidebar

Related Questions

thanks for looking, I've had a terrible time trying to get the right search
Hi and thanks for looking. I need to get all form inputs from a
Thanks for looking. I am trying to get the smallest time between visits per
thanks for looking my problem is that i cant seem to get jquery to
thanks for looking. I'm having an issue i cant seem to get the submit
Thanks for looking at my question. I want to get the mobile version by
Thanks for looking. I'm trying to put together a simple live wallpaper, and trying
Thanks for looking into this. Basically, what I am trying to do is to
Hello everyone thanks for looking into my problem. What I am trying to do
Hey guys, and thanks for looking. I have built the simple D2D app from

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.