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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:39:38+00:00 2026-05-18T11:39:38+00:00

I have the following table (contains over a million rows): **MyTable** id – int(10)

  • 0

I have the following table (contains over a million rows):

**MyTable**
id - int(10) 
externalId - smallint(5)
description - varchar(250)
status - smallint(6)
entrydate - int(10) (unix timestamp)

I would like to be able to select a single random row (I only need the id value in the resultset) from this table with the following where clause:

/* The number of external ids here can range from 1 to around 50 */
WHERE externalId in (1,2,3,4,x,x,x)
/* Status is fixed at 2 for all calls */
AND status = 2
/* Entry date is typically (now - 30 days) or may be excluded all together */
AND entrydate > 1279887765

The obvious solution is to use rand() however I have found that the performance of this is somewhat lacking and this is fairly well documented. So does anybody have any alternative solutions?

To give a little background: I intend to use this query on a PHP driven website and the input parameters depend on the user so these will change from user to user. It is also worth mentioning that I cannot change the table structure at all.

Thanks in advance.

  • 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-18T11:39:38+00:00Added an answer on May 18, 2026 at 11:39 am

    You could do two queries – one to get the count of total possible rows, then use that count to generate a random integer from zero to ($count – 1) in PHP, and then add “LIMIT $rand, 1” to the end of the second query.

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

Sidebar

Related Questions

I have a table that contains the following columns: ID int, DISTANCE float, EVENT
I have the following requirement. I have a table with a column that contains
i have a table Students which contains the following colums: Id,FirstName,LastName,Adress . The colum
I have a table of surveys which contains (amongst others) the following columns survey_id
I have a Model created with a table called Customers. It contains the following
Let's say I have the following tables: PartyRelationship EffectiveDatedAttributes PartyRelationship contains a varchar column
I have the following table that contains checkboxes in each row. When checkbox status
I have a table which contains many rows, each with a column named RecordData
I have the following situation (in Rails 3): my table contains financial transactions for
I have a SQL table with the following schema: fruit_id INT price FLOAT date

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.