Solved

Mysql rand()

Posted on 2012-03-28
3
532 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…

751 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