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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:52:45+00:00 2026-06-17T16:52:45+00:00

Trying to generate a unique code to prevent duplicates i use the following query

  • 0

Trying to generate a unique code to prevent duplicates i use the following query using HAVING clause so i can use the alias but i get duplicate key errors:

SELECT
  FLOOR(100 + RAND() * 899) AS random_code 
FROM product_codes 
HAVING random_code NOT IN (values) 
LIMIT 1

The following code did not work and is what i need:

https://stackoverflow.com/a/4382586

Is there a better way to accomplish this or there is something wrong in my query?

  • 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-17T16:52:46+00:00Added an answer on June 17, 2026 at 4:52 pm

    Wanted to use a MYSQL query to get random numbers left between 0-999 as a code but tryed that query and also i ended filling the values condition from 0 to 999 and still got always duplicate codes, strange behaviour so i ended up using PHP.

    The steps i use now:

    Create an array populated with 0 to 999, in the future if i need more codes will use 0 to 9999.

    $ary_1 = array(0, 1, 2, …., 999)

    Create other array populated with all codes in the table.

    $ary_2 = array(4, 5, 985, 963, 589)

    Get a resulting array using array_diff.

    $ary_3 = array_diff($ary_1, $ary_2)

    Get an array key using array_rand from the resulting array_diff.

    $new_code_key = array_rand($ary_3, 1)

    Use that key to get the code and create the MYSQL query.

    $ary_3[$new_code_key]

    Unset that key from the new codes array so i speed up the process and just have to get anoher array_rand key and unset later.

    unset($ary_3[$new_code_key])

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

Sidebar

Related Questions

I'm trying to generate the following html code using cl-who: <html> <body> <div id=cnt_1></div>
i'm trying to generate a unique id using this code: $RCode = md5(uniqid(rand(), true));
I'm trying to generate a random Product list using the following method, but I'm
Im trying to generate a color gradient using ColdFusion. My current code below works
I am trying to generate images using ImageMagick, please check the code below: <?
I'm trying to generate unique permutations of an array using Math::Combinatorics. As CPAN page
I am trying with below code to generate 10 digits unique random number. As
am having a problem with a bit of code. I am trying to generate
I am trying to generate unique values in c# with the help of DateTime
I'm trying to generate 16 000 000 unique random numbers (10-digits: range 1 000

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.