Mysql inserts a random number into a field (inserts a random number to the MySQL database) and a random number to mysql

Source: Internet
Author: User

Mysql inserts a random number into a field (inserts a random number to the MySQL database) and a random number to mysql

Common Code

UPDATE 'table name 'set' field name' = ceiling (rand () * 500000 + 500000) WHERE (condition );
Update 'table name' set click = click * 0.01 where classid = '2' and click> 2000

We often encounter the use of random, the following is a method to solve the random number.

When constructing test data, we need to insert random data into the test table. The following describes how to construct test data: update

Step 1: The SQL function of the random number is rand (), while rand () generates decimal places between 0 and 1.

Step 2: Set rand () * 10

A decimal number between 1 and 10 can be converted using ceil.

Step 3: Use cast for type conversion

Cast is used as follows:

Cast (value as type)

Binary, with a BINARY Prefix: binary

Character type, with parameters: CHAR ()

DATE: DATE

TIME: TIME

DATETIME type

Floating Point: DECIMAL

Integer: SIGNED

UNSIGNED integer

Select cast (RAND () * 10 AS signed) AS rand

Step 4: run the SQL statement

UPDATE storm_effect_info s SET

S. fhtp = (select cast (RAND () * 10 AS signed) AS rand) WHERE s. id = 13

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.