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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:47:29+00:00 2026-05-31T01:47:29+00:00

I have a MySQL query that results in something like this: person | some_info

  • 0

I have a MySQL query that results in something like this:

person | some_info 
==================
   bob | pphsmbf24
   bob | rz72nixdy
   bob | rbqqarywk
  john | kif9adxxn
  john | 77tp431p4
  john | hx4t0e76j
  john | 4yiomqv4i
  alex | n25pz8z83
  alex | orq9w7c24
  alex | beuz1p133
   etc...

(This is just a simplified example. In reality there are about 5000 rows in my results).

What I need to do is go through each person in the list (bob, john, alex, etc…) and pull out a row from their set of results. The row I pull out is sort of random but sort of also based on a loose set of conditions. It’s not really important to specify the conditions here so I’ll just say it’s a random row for the example.

Anyways, using PHP, this solution is pretty simple. I make my query and get 5000 rows back and iterate through them pulling out my random row for each person. Easy.

However, I’m wondering if it’s possible to get what I would from only a MySQL query so that I don’t have to use PHP to iterate through the results and pull out my random rows.

I have a feeling it might involve a BUNCH of subselects, like one for each person, in which case that solution would be more time, resource and bandwidth intensive than my current solution.

Is there a clever query that can accomplish this all in one command?

Here is an SQLFiddle that you can play with.

  • 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-31T01:47:31+00:00Added an answer on May 31, 2026 at 1:47 am

    To get a random value for a distinct name use

    SELECT r.name, 
    (SELECT r1.some_info FROM test AS r1 WHERE r.name=r1.name ORDER BY rand() LIMIT 1) AS     'some_info' 
    FROM test AS r 
    GROUP BY r.name ;  
    

    Put this query as it stands in your sqlfiddle and it will work

    Im using r and r1 as table alias names. This will also use a subquery to select a random some_info for the name

    SQL Fiddle is here

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

Sidebar

Related Questions

Let's say that I have a bunch of records in MySQL, something like this:
I'm running an MySQL query that returns results based on location. However I have
I have a method that basically returns the results of a mysql query. public
I have a MySQL query that I would like to optimize a little bit.
I have a mysql db query that I'd like to take and condense into
I have a table that looks something like this: CREATE TABLE student_results(id integer, name
I have a few lines of code that start something like this: $trailheads =
I have some MySQL results like this: --------------------------- | name | something_random | ---------------------------
I have a MySQL function that looks like this: delimiter # create function activate_user
I have a MySQL database with a few tables. They look something like this

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.