Rand generates random number code in MySQL

Source: Internet
Author: User
Tags function definition mysql tutorial php tutorial rand time 0

To randomly generate string code as follows:

Define a random string method in the MySQL tutorial, and then call this method in the SQL statement.

Random string function definition method:
Select Round (Round (rand (), 4) *10000);
It would be nice not to use the function directly.

If you have to write a function,
Delimiter $$

Create
function Get_rand ()
returns int deterministic
Begin
declare return_no int;
Select Round (Round (rand (), 4) *10000) into Return_no;
return return_no;
end$$

delimiter;

Examples of using random string function methods:
Update demotable set demoname=rand_string where id>23
Direct execution can be done.


I was at the beginning of the test, because I did not add min (id) judgment, the result is half of the time is always query to the first few lines in the table.
The full query statement is:
SELECT * from ' table '
Where ID >= (select floor (ID) from ' table ') + (select min (ID) () () + (()) + (() () + (() () () () + ("()") M ' table '))
Order by ID limit 1;
SELECT *
From ' table ' as T1 join (select round (select MAX (ID) from ' table ') (select min (IDs) from ' tables ')) + (select min () d) from ' table ') as ID) as T2
where T1.id >= t2.id
ORDER by t1.id limit 1;
Finally, in the PHP tutorial, the two statements were queried 10 times,
The former takes 0.147433 seconds.
The latter takes time 0.015130 seconds

That's the way the MySQL rand function implements random numbers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.