Solved

Mysql rand()

Posted on 2012-03-28
3
531 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 83

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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

685 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