robert donwwawer
robert donwwawer

Reputation: 15

How to move data between table by random position?

How to move data between table by random position ?

I use this code for move all data from table xxx to table yyy order by id asc, It's work good.

<?PHP
include("connect.php");
$sql = "INSERT INTO yyy
        (name , last_name , tel_no) 
SELECT 
         name , last_name , tel_no
FROM xxx order by id asc";
$dbQuery = mysql_query($sql);
?>

But now i want to move all data from table xxx to table yyy by random position (not order by id asc), What is the best way to do that ?

Upvotes: 1

Views: 81

Answers (2)

Ben
Ben

Reputation: 52893

When selecting data from a table there are no guarantees about the order the data is returned in without an ORDER BY statement. Order is not a property of a table and it's entirely possible that performing the same SELECT twice will not return data in the same order.

Thus, though you have inserted data into your table with an order you will never be able to guarantee that you can get it out of the table in the same order. It's therefore pointless to do this.

When you SELECT the data, i.e. when you want to perform some business logic or you want to display the data to a human, you should add whatever sort order you want. If you want to order it randomly then you can use the RAND() function, which returns a random floating point value.

select * from xxx order by rand()

Upvotes: 1

m_pro_m
m_pro_m

Reputation: 340

You can use RAND() function to order rows randomly

ORDER BY RAND()

Upvotes: 1

Related Questions