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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:41:27+00:00 2026-06-11T16:41:27+00:00

For fetching records from a table I use this mysql query: SELECT a.id as

  • 0

For fetching records from a table I use this mysql query:

SELECT 
    a.id as aid, a.data1 as adata1, a.data2 as adata2
    b.id as bid, b.data1 as bdata1, b.data2 as bdata2
FROM table AS a
JOIN table AS b ON ( a.id <> b.id ) 
WHERE (a.data1=1 AND b.data1=1) AND ABS( a.rating - b.rating ) <100
ORDER BY RAND() 
LIMIT 1

This query fetching exactly the records which I need, but unfortunately because of RAND() is this query quite slow.

I’ve found some ways, how to avoid using RAND() function, for example here. But my problem is, that I still cannot find a way, how to replace RAND() function in this query.
In some simple query is not problem to replace RAND(), but I don’t know, how to do that in the example above… because of more conditions in the WHERE clause.

  • 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-11T16:41:28+00:00Added an answer on June 11, 2026 at 4:41 pm

    Since you are using MySQL you can try with the following SQL queries that first gets a count from the table, then selects a random offset based on that count. It then prepares a statement so the calculated offset can be used and executes the statement.

    SELECT @count := COUNT(*) FROM table AS a JOIN table AS b ON ( a.id <> b.id ) WHERE (a.data1=1 AND b.data1=1) AND ABS( a.rating - b.rating ) <100;
    SET @offset = CONVERT(FLOOR(RAND() * @count), SIGNED);
    PREPARE mystatement FROM "SELECT 
                              a.id as aid, a.data1 as adata1, a.data2 as adata2
                              b.id as bid, b.data1 as bdata1, b.data2 as bdata2
                              FROM table AS a
                              JOIN table AS b ON ( a.id <> b.id ) 
                              WHERE (a.data1=1 AND b.data1=1) AND ABS( a.rating - b.rating ) <100 LIMIT ?, 1";
    EXECUTE mystatement USING @offset;
    DEALLOCATE PREPARE mystatement;
    

    On a large dataset should perform faster than ORDER BY RAND(), try and let me know … 😉

    EDIT

    The queries will not work used on phpmyadmin, so run them using the MySQL console or write a php script in which you have two option, the first one is let mysql do the work :

    mysql_query('SELECT @count := COUNT(*) FROM table AS a JOIN table AS b ON ( a.id <> b.id ) WHERE (a.data1=1 AND b.data1=1) AND ABS( a.rating - b.rating ) <100');
    mysql_query('SET @offset = CONVERT(FLOOR(RAND() * @count), SIGNED)');
    mysql_query('PREPARE mystatement FROM "SELECT 
                              a.id as aid, a.data1 as adata1, a.data2 as adata2
                              b.id as bid, b.data1 as bdata1, b.data2 as bdata2
                              FROM table AS a
                              JOIN table AS b ON ( a.id <> b.id ) 
                              WHERE (a.data1=1 AND b.data1=1) AND ABS( a.rating - b.rating ) <100 LIMIT ?, 1"');
    $res = mysql_query('EXECUTE mystatement USING @offset');
    $row = mysql_fetch_assoc($res);
    print_r($row);
    

    The second option that could be even more faster consist of doing a part of the work with MySQL and the other part with the programming language (in our case PHP) :

    $res = mysql_query("SELECT COUNT(*) FROM table AS a JOIN table AS b ON ( a.id <> b.id ) WHERE (a.data1=1 AND b.data1=1) AND ABS( a.rating - b.rating ) <100')");
    $row = mysql_fetch_array($res);
    $offset = rand(0, $row[0]-1);
    
    $res = mysql_query("SELECT 
                                  a.id as aid, a.data1 as adata1, a.data2 as adata2
                                  b.id as bid, b.data1 as bdata1, b.data2 as bdata2
                                  FROM table AS a
                                  JOIN table AS b ON ( a.id <> b.id ) 
                                  WHERE (a.data1=1 AND b.data1=1) AND ABS( a.rating - b.rating ) <100 LIMIT $offset, 1");
    $row = mysql_fetch_assoc($res);
    

    Another alternative way to speed up the ORDER BY RAND() that I’ve found consist in a query like the following :

    SELECT 
        a.id as aid, a.data1 as adata1, a.data2 as adata2
        b.id as bid, b.data1 as bdata1, b.data2 as bdata2
    FROM table AS a
    JOIN table AS b ON ( a.id <> b.id ) 
    WHERE (RAND() < (SELECT ((1/COUNT(*))*10) FROM table AS a JOIN table AS b ON ( a.id <> b.id ) ) )
     AND (a.data1=1 AND b.data1=1) AND ABS( a.rating - b.rating ) <100
    ORDER BY RAND() 
    LIMIT 1
    

    Don’t forget to update me about the result you get 😉 .

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

Sidebar

Related Questions

I am fetching all records from a db table into a page using; SELECT
Consider fetching data with SELECT * FROM table WHERE column1='XX' && column2='XX' Mysql will
I have a table and from that I am fetching records somewhere around 250,000
I am fetching records from one table with count of one field with other
I have table from which I am fetching records for two time interval as
I am using this query: SELECT * from likes GROUP BY url ORDER BY
Let say i have 100k records in table, after fetching that records from table
I want to use order by with union in mysql query. I am fetching
I am fetching records from my record table. record table has many columns tow
How to create a dynamic table from java script?I am fetching a record from

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.