Ziva
Ziva

Reputation: 3501

MySQL - rand from the defined values

I'm trying to insert random values to tables. But I don't want to make new values totally random (like eg: random numbers or characters),I want to take a random value from a set of values defined earlier. For example: defined values: xyz, abc, edf random value: abc

I programming languages it's very easy, but in SQL I don't know, how to store this defined values and how to take random value.

Upvotes: 2

Views: 103

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269773

Put your random values in a table.

Then, you can insert them one at a time by doing:

insert into tables(val)
    select val
    from randomtable
    order by rand()
    limit 1

The order by rand() limit 1 chooses a random value from your list.

Upvotes: 2

Related Questions