Quinton Thompson
Quinton Thompson

Reputation: 61

How to generate random 10 digit number and place into database table?

I am working with a MySQL database that has a table of customers. One of the columns consist of phone_numbers varchar(10). My question is how can I create a simple loop that goes through all the customers and inserts a random phone numbers?

customer table

Upvotes: 2

Views: 6080

Answers (1)

RetroCraft
RetroCraft

Reputation: 337

Use an UPDATE statement with the RAND and LPAD functions (docs: RAND, LPAD).

Example (generates numbers from 0000000000 to 9999999999): UPDATE table SET phone_numbers = LPAD(FLOOR(RAND() * 10000000000), 10, '0')

Upvotes: 6

Related Questions