The code to randomly generate a string is as follows:
Define a random string method in the mysql tutorial, and then call this method in the SQL statement.
Method for defining random string functions:
Select round (rand (), 4) * 10000 );
You can simply do this without using a function.
If you have to write a function
Delimiter $
Create
Function get_rand ()
Returns int deterministic
Begin
Declare return_no int;
Select round (rand (), 4) * 10000) into return_no;
Return return_no;
End $
Delimiter;
Example of using a random string function:
Update demotable set demoname = rand_string (32) where id> 23
Run the command directly.
At the beginning of the test, because I did not add the min (id) Judgment, half of the time is always the first few rows in the table.
The complete query statement is:
Select * from 'table'
Where id> = (select floor (rand () * (select max (id) from 'table')-(select min (id) from 'table ')) + (select min (id) from 'table ')))
Order by id limit 1;
Select *
From 'table' as t1 join (select round (rand () * (select max (id) from 'table')-(select min (id) from 'table ')) + (select min (id) from 'table') as id) as t2
Where t1.id> = t2.id
Order by t1.id limit 1;
Finally, in the php tutorial, query the two statements 10 times respectively,
The former takes 0.147433 seconds.
The latter takes 0.015130 seconds.
The above is how the mysql rand function implements random numbers.