Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I update a fields in table with strings randomly selected from a known list?

I have a table named "buildings" that contains a varchar(50) field named "use". The table already has several thousand records, however the "use" values are all null. I would like to update these this table with randomly chosen values from a list of strings (e.g., warehouse, office, market, retail, workshop). I would also like to leave some of these null to emulate real world usage.

How can I update a field in a table with strings randomly selected from a known list?

like image 628
Ryan Taylor Avatar asked Mar 02 '23 02:03

Ryan Taylor


1 Answers

This might work for you:

BEGIN;
UPDATE Buildings SET Use = (ARRAY['warehouse', 'office', 'market', 'retail', 'workshop', NULL])[floor(random() * 6.0) + 1];
COMMIT;
like image 119
calebbrown Avatar answered Apr 05 '23 22:04

calebbrown