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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:38:40+00:00 2026-05-30T15:38:40+00:00

I have some MySQL results like this: ————————— | name | something_random | —————————

  • 0

I have some MySQL results like this:

---------------------------
| name | something_random |
---------------------------
| john | ekjalsdjalfjkldd |
| alex | akjsldfjaekallee |
| alex | jkjlkjslakjfjflj |
| alex | kajslejajejjaddd |
|  bob | ekakdie33kkd93ld |
|  bob | 33kd993kakakl3ll |
| paul | 3k309dki595k3lkd |
| paul | 3k399kkfkg93lk3l |
etc...

This goes on for 1000’s of rows of results. I need to limit the number of results to the first 50 unique names. I think there is a simple solution to this but I’m not sure.

I’ve tried using derived tables and variables but can’t quite get there. If I could figure out how to increment a variable once every time a name is different I think I could say WHERE variable <= 50.

UPDATED

I’ve tried the Inner Join approach(es) suggested below. The problem is this:

The subselect SELECT DISTINCT name FROM testTable LIMIT 50 grabs the first 50 distinct names. Perhaps I wasn’t clear enough in my original post, but this limits my query too much. In my query, not every name in the table is returned in the result. Let me modify my original example:

----------------------------------
|   id | name | something_random |
----------------------------------
|    1 | john | ekjalsdjalfjkldd |
|    4 | alex | akjsldfjaekallee |
|    4 | alex | jkjlkjslakjfjflj |
|    4 | alex | kajslejajejjaddd |
|    6 |  bob | ekakdie33kkd93ld |
|    6 |  bob | 33kd993kakakl3ll |
|   12 | paul | 3k309dki595k3lkd |
|   12 | paul | 3k399kkfkg93lk3l |
etc...

So I added in some id numbers here. These ID numbers pertain to the people’s names in the tables. So you can see in the results, not every single person/name in the table is necessarily in the result (due to some WHERE condition). So the 50th distinct name in the list will always have an ID number higher than 49. The 50th person could be id 79, 234, 4954 etc…

So back to the problem. The subselect SELECT DISTINCT name FROM testTable LIMIT 50 selects the first 50 names in the table. That means that my search results will be limited to names that have ID <=50, which is too constricting. If there are certain names that don’t show up in the query (due to some WHERE condition), then they are still counted as one of the 50 distinct names. So you end up with too few results.

UPDATE 2

To @trapper: This is a basic simplification of what my query looks like:

SELECT
    t1.id,
    t1.name,
    t2.details
FROM t1
LEFT JOIN t2 ON t1.id = t2.some_id
INNER JOIN
    (SELECT DISTINCT name FROM t1 ORDER BY id LIMIT 0,50) s ON s.name = t1.name
WHERE
    SOME CONDITIONS
ORDER BY
    t1.id,
    t1.name

And my results look like this:

----------------------------------
|   id | name |          details |
----------------------------------
|    1 | john | ekjalsdjalfjkldd |
|    3 | alex | akjsldfjaekallee |
|    3 | alex | jkjlkjslakjfjflj |
|    4 | alex | kajslejajejjaddd |
|    6 |  bob | ekakdie33kkd93ld |
|    6 |  bob | 33kd993kakakl3ll |
|   12 | paul | 3k309dki595k3lkd |
|   12 | paul | 3k399kkfkg93lk3l |
...
|   37 | bill | kajslejajejjaddd |
|   37 | bill | ekakdie33kkd93ld |
|   41 | matt | 33kd993kakakl3ll |
|   50 | jake | 3k309dki595k3lkd |
|   50 | jake | 3k399kkfkg93lk3l |
----------------------------------

The results stop at id=50. There are NOT 50 distinct names in the list. There are only roughly 23 distinct names.

  • 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-30T15:38:42+00:00Added an answer on May 30, 2026 at 3:38 pm

    My MySql syntax may be rusty, but the idea is to use a query to select the top 50 distinct names, then do a self-join on name and select the name and other information from the join.

    select a.name, b.something_random
    from Table b
        inner join (select distinct name from Table order by RAND() limit 0,50) a
             on a.name = b.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL query like this: SELECT *, SUM(...some SQL removed for brevety)
I have a MySQL query that results in something like this: person | some_info
I have some text stored in MySQL database like this Tom gave Sarah £5
I have some data, yes, data. This data came from a MySQL query and
Let's say that I have a bunch of records in MySQL, something like this:
I have some code that creates a table like this: for($i=0; $i<$fields_num; $i++) {
Greetings, I have some mysql tables that are currently using an md5 hash as
I have some code which needs to ensure some data is in a mysql
I have some financial values stored as text in a mysql db. the significance
I am running Ruby and MySQL on a Windows box. I have some Ruby

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.