Solved

Mysql rand()

Posted on 2012-03-28
3
521 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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
Comment Utility
thanks mate, that was amazing, it did the trick
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now