Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate Random numbers with fixed digits in Mysql

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to generate fixed-digit random numbers in Mysql. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The project needs to generate some fixed-digit random numbers dynamically and randomly, such as 8-bit, 5-bit and so on.

The way I saw it before is like this.

ROUND (ROUND (RAND (), 5) * 100000)

The Rand () function is a random function with 0 ~ 1 (infinitely close).

If a random number takes out 0.05321

So what comes out in this way is 5321, only four.

If you can wrap it with a function and find that the number of digits is wrong after taking the value, it will be more perfect to fill it in.

The following is a function I modified, but the disadvantage is that the number of bits generated cannot exceed 20 digits. Of course, it's okay to change it.

DELIMITER $$

USE `prvecard` $$

DROP FUNCTION IF EXISTS `getRand` $$

CREATE `FUNCTION `getRand` (counts INTEGER) RETURNS VARCHAR (20) CHARSET utf8

BEGIN

DECLARE sTemp VARCHAR (20)

DECLARE sTempCounts INTEGER

SET sTemp = CONCAT (ROUND (ROUND (RAND (), counts) * (POW (10 points),)

IF (CHAR_LENGTH (sTemp))

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report