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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:56:08+00:00 2026-06-12T00:56:08+00:00

Possible Duplicate: How to request a random row in SQL? Because of a huge

  • 0

Possible Duplicate:
How to request a random row in SQL?

Because of a huge table I cannot use MySQL function RAND() and trying to avoid using RAND() this way:

$a = mysql_fetch_array(mysql_query("SELECT MIN(id), MAX(id) FROM table WHERE category_id='".$var."'"));
$id = rand($a[0],$a[1]);
$id2 = rand($a[0],$a[1]);

Which doesn’t work, because: in $a I have the biggest and smallest ID with the respective category_id – that’s ok.

The problem brings the rows with $id and $id2 – because there are used the minimal and maximal ID values, but PHP function rand() will take some random value between minimum and maximum – and there is a big probability, that the picked out record will not belongs into category_id.

So I would like to ask you about a way, how could I fix this example.

THank you

  • 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-12T00:56:09+00:00Added an answer on June 12, 2026 at 12:56 am

    You could try this:

    $row = mysql_fetch_assoc(
        mysql_query(
          "SELECT count(*) as count FROM table WHERE category_id='$var'"));
    
    $randomrow = rand(0, $row['count'] -1);
    
    $q = mysql_query("
      SELECT * FROM table 
      WHERE category_id='$var'
      LIMIT 1 OFFSET $randomrow");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to request a random row in SQL? Currently Using: $randomQuery =
Possible Duplicate: How to request a random row in SQL? Is this the correct
Possible Duplicate: How to retrieve JSON via ASP.Net context.Request Im trying to parse an
Possible Duplicate: web service request call SOAP request missing empty parameters I'm trying to
Possible Duplicate: PHP detecting request type (GET, POST, PUT or DELETE) This should be
Possible Duplicate: JavaScript post request like a form submit I have a value calculated
Possible Duplicate: Where are the request method constants in the Servlet API? I'm looking
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: Servlets: doGet and doPost I know doGet() request is appended to the
Possible Duplicate: Create an alert on any view controller after Facebook request:didFailWithError: I have

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.