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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:10:32+00:00 2026-06-02T11:10:32+00:00

I would like get 3 random stamps, but each from different country. This query

  • 0

I would like get 3 random stamps, but each from different country. This query return random stamps, but could be from the same country. When I add GROUP BY country_id I will get 3 stamps of different countries, but only first stamps of each country.

SELECT `stamps`.`stamp_id`, `countries`.`country_name_cs` FROM `stamps`
LEFT JOIN `countries` ON countries.country_id = stamps.country_id
WHERE (stamps.stamp_enabled = 1) ORDER BY rand() ASC LIMIT 3

Any idea?

Query profile (#Ezequiel Muns solution)

starting              0.000232
Opening tables        0.000047
System lock           0.000013
Table lock            0.000635
optimizing            0.000036
statistics            0.000022
preparing             0.000023
Creating tmp table    0.000293
executing             0.000004
Copying to tmp table  0.060066
Sorting result        0.013835
Sending data          0.089164
removing tmp table    0.000632
Sending data          0.000026
init                  0.000048
optimizing            0.000014
statistics            0.000061
preparing             0.000028
Creating tmp table    0.000326
executing             0.000004
Copying to tmp table  0.353176
Sorting result        0.000158
Sending data          0.000038
end                   0.000005
removing tmp table    0.000018
end                   0.000006
query end             0.000004
freeing items         0.000575
removing tmp table    0.002363
closing tables        0.000023
logging slow query    0.000004
cleaning up           0.000009

id      select_type       table      type       possible_keys       key         key_len     ref           rows      Extra
1       PRIMARY       <derived2>     ALL        NULL                NULL        NULL        NULL          12679     Using temporary; Using filesort
1       PRIMARY       c              eq_ref     PRIMARY             PRIMARY     4           s.country_id  1      
2       DERIVED       stamps         ALL        NULL                NULL        NULL        NULL          12679     Using where; Using temporary; Using filesort
  • 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-02T11:10:35+00:00Added an answer on June 2, 2026 at 11:10 am

    You can try the following query, this selects 3 random countries first, performs a join with a mixed stamp table, and then groups by country. Not efficient, but workable for smaller data sets.

     SELECT
        mixed_stamp.stamp_id,
        random.country_name_cs
     FROM
        (SELECT * FROM stamps ORDER BY RAND()) AS mixed_stamp
     LEFT JOIN (SELECT country_id FROM countries ORDER BY RAND() LIMIT 3) random ON (random.country_id = mixed_stamp.country_id)
     GROUP BY random.country_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to get 2 random different elements from an std::vector. How can
I would like to get data from from different webpages such as addresses of
I would like to get a list of the wireless networks available. Ideally this
I would like to create a custom Android button from 9patches, but without using
Let say that I would like to get an random double value wich is:
How do I get a random post in Wordpress? I would like to display
I would like to know if there is a way to get a random
I would like to know how to implement a way to get a random
I would like to compare two different sets of data on the same imshow
Would like to get a list of advantages and disadvantages of using Stored Procedures.

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.