1

I'm working on a project that requires back-end service. I am using MySQL and php scripts to achieve communication with server side. I would like to add a new feature on the back-end and that is the ability to generate automatically a table with 3 'lucky' members from a table_members every day. In other words, I would like MySQL to pick 3 random rows from a table and add these rows to another table (if is possible). I understand that, I can achieve this if manually call RAND() function on that table but ... will be painful!

There is any way to achieve the above?

UPDATE:

Here is my solution on this after comments/suggestions from other users

CREATE EVENT `draw` ON SCHEDULE EVERY 1 DAY STARTS '2013-02-13 10:00:00' ON COMPLETION NOT PRESERVE ENABLE DO 
INSERT INTO tbl_lucky(`field_1`) 
SELECT u_name 
FROM tbl_members 
ORDER BY RAND() 
LIMIT 3

I hope this is helpful and to others.

4

1 に答える 1

1

You can use the INSERT ... SELECT and select 3 rows ORDER BY RAND() with LIMIT 3

For more information about the INSERT ... SELECT statement - see

It's also possible to automate this every day job with MySQL Events(available since 5.1.6)

于 2013-02-12T23:24:24.357 に答える