Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Mysql rand()

Posted on 2012-03-28
3
Medium Priority
?
537 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 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 Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
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…

730 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