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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:43:01+00:00 2026-06-15T17:43:01+00:00

I want to have a PHP code to draw some ids one by one

  • 0

I want to have a PHP code to draw some ids one by one without replacement randomly from a table in MySQL database.

Now I implement it like this:

There is a table called box

CREATE TABLE box
(
id INT UNSIGNED AUTO_INCREMENT,
PRIMARY KEY (id)
);

Say it contains 15 records,

box

id:
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
=====

The ids that have been drawn will be recorded and excluded in next draw.

For example,

at the start, the SQL for drawing will be

SELECT id FROM box RAND() LIMIT 1;`.

Say the result is 4.

Then I have drawn 4, the next SQL for drawing will be

SELECT id FROM box WHERE id != 4 ORDER BY RAND() LIMIT 1;

Say the result is 6 this times.

That is, I have drawn 4 and 6, so the next SQL for drawing will be

SELECT id FROM box WHERE id != 4 AND id != 6 ORDER BY RAND() LIMIT 1;"

The problem is that:

If the table ‘box’ contains 2000 records and I have drawn 1000+ ids, the next SQL for drawing will be very long. I wonder would a longer SQL statment make the program or the process in MySQL run slower? If yes, is there a solution/design/algorithm to solve this problem? The records in the table may increase so I cannot make a random sequence on ids before the drawing.


More about the problem:

the PHP code will be called by multiple users, so each user has some different ids that have been drawn by him/her.

Actually, in my current design, the ids that have been drawn by a user are stored in PHP session variable. When a user wants to draw, the ids stored in session variable will be accessed for the drawing SQL as above.

  • 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-15T17:43:02+00:00Added an answer on June 15, 2026 at 5:43 pm

    Most of the queries written will take approximately the same time as your query.

    By the way the conditions in your query are equivalent to a NOT IN and it may easier for you to apply a NOT IN

    SELECT * FROM box WHERE id NOT IN (a[0],a[1]...)
    

    Now as to whether the increased size of the query will slow down MySQL the answer is no, not really.

    An article about why it won’t slow it down is here (remember your query is equivalent to a not in)

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

Sidebar

Related Questions

I want to have a page run some PHP code when a user clicks
Everything works with this code but i want to have a link from product.php
I have some php code in a database like so $x = <?php some
I have PHP code to do a simple MySQL database search with only two
I have some php code that generates multiple MySql queries and combines the results.
I use the smarty tpl. Now i have a php code and i want
I have the following php code which I want to add a delay too:
I have php mysql application and i want the phone numbers be saved in
I have a PHP page and I want to share some data between pages
I have a php table and i want the ability to delete rows of

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.