David542
David542

Reputation: 110143

Get random array value in mysql

I am trying to create a mock dataset to test some queries on. How would I get a random selection from an array in mysql. For example:

select id, random(['microsoft', 'chrome', 'firefox']) browser from mytable

Upvotes: 4

Views: 3095

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269643

You can use elt() and random():

select id,
       elt(floor(rand() * 3 + 1), 'microsoft', 'chrome', 'firefox') browser
from mytable;

Upvotes: 11

Related Questions