Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Mysql rand()

Posted on 2012-03-28
3
Medium Priority
?
546 Views
Last Modified: 2012-03-29
Hi Guys,

Quick question, can you randomly distribute strings in a column, i know this can be done with numbers but not sure with words.

example - randomise - "new", "used" between 300 rows

i.e
book_title                       condition
php for dummies                 new
php in practice                     used
amazon for dummies          used
etc.............

thanks in advance.
0
Comment
Question by:MK15
3 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37778134
If you have an auto-increment id column, you can use the rand() function against it to select 'random' items.  Two problems with that are that you may get duplicates and you may not show all items because some may never come up.  It will take a little code because rand() returns a floating point value.  There is code here to get an int:

http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand
0
 
LVL 24

Accepted Solution

by:
johanntagle earned 2000 total points
ID: 37780039
update table_name set `condition`=(select a.b from (select 'new' as b union select 'old' as b) a order by rand() limit 1);

See it in action below (I just used book_id to quickly create a test table, but the ending SQL should work with any table:
mysql> create table book_test (book_id int not null auto_increment primary key, `condition` varchar(10));
Query OK, 0 rows affected (0.08 sec)

mysql> insert into book_test (`condition`) values (null),(null),(null),(null),(null),(null),(null),(null),(null);
Query OK, 9 rows affected (0.00 sec)
Records: 9  Duplicates: 0  Warnings: 0

mysql> select * from book_test;
+---------+-----------+
| book_id | condition |
+---------+-----------+
|       1 | NULL      |
|       2 | NULL      |
|       3 | NULL      |
|       4 | NULL      |
|       5 | NULL      |
|       6 | NULL      |
|       7 | NULL      |
|       8 | NULL      |
|       9 | NULL      |
+---------+-----------+
9 rows in set (0.00 sec)

mysql> update book_test set `condition`=(select a.b from (select 'new' as b union select 'old' as b) a order by rand() limit 1);
Query OK, 9 rows affected (0.00 sec)
Rows matched: 9  Changed: 9  Warnings: 0

mysql> select * from book_test;
+---------+-----------+
| book_id | condition |
+---------+-----------+
|       1 | old       |
|       2 | new       |
|       3 | new       |
|       4 | old       |
|       5 | new       |
|       6 | old       |
|       7 | old       |
|       8 | new       |
|       9 | old       |
+---------+-----------+
9 rows in set (0.00 sec)

Open in new window

Looks random.  Let's see it run again:
mysql> update book_test set `condition`=(select a.b from (select 'new' as b union select 'old' as b) a order by rand() limit 1);
Query OK, 5 rows affected (0.00 sec)
Rows matched: 9  Changed: 5  Warnings: 0

mysql> select * from book_test;
+---------+-----------+
| book_id | condition |
+---------+-----------+
|       1 | old       |
|       2 | new       |
|       3 | old       |
|       4 | new       |
|       5 | new       |
|       6 | new       |
|       7 | old       |
|       8 | old       |
|       9 | new       |
+---------+-----------+
9 rows in set (0.00 sec)

Open in new window

0
 

Author Comment

by:MK15
ID: 37782456
thanks mate, that was amazing, it did the trick
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question