Inserting random numbers into MySQL database

Source: Internet
Author: User
Tags rand

  

We often encounter the problem of using random numbers, and here is a way to solve a random number.

When constructing the test data, we need to insert random data into the test table. The method of constructing the test data is as follows, only the update example illustrates

Step 1: The SQL function for the random number is rand (), and Rand () generates a decimal between 0-1.

  

Step 2: Put rand () *10

Step 3: Use cast for type conversion

The use of cast is as follows:

Cast (value as type)

Binary, with binary prefix effect: binary

Character type, with parameters: CHAR ()

Date: Date

Time:

DateTime Date/Time type

Floating point number: DECIMAL

Integer: Signed

unsigned integer: UNSIGNED

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

Inserting random numbers into MySQL database

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.