4

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

1 Answer 1

11

You can use elt() and random():

select id,
       elt(floor(rand() * 3 + 1), 'microsoft', 'chrome', 'firefox') browser
from mytable;
Sign up to request clarification or add additional context in comments.

4 Comments

@David542 . . . It chooses a value from a list.
@GordonLinoff thanks -- what does "ELT" stand for? (not extract-load-transform, is it?)
@David542 . . . My guess would be "element".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.