web-nomad
web-nomad

Reputation: 6003

Randomise strings in mysql

I've a string in mysql that i want to randomise so that every time i run the query, i get different results.

The sample string

$str = '4_127','2_84','2_85';

So, i will pass it to the ORDER BY clause to randomise results.

SELECT `MY_SEARCH_PARAMS` FROM `mytable` WHERE `MY_WHERE_CONDTIONS` ORDER BY
FIELD( CONCAT( property_id,"_",catalog_id ), '4_127','2_84','2_85' ), `id` ASC;

I need a way so that the order of the string contents is changed everytime.

Thanks.

Upvotes: 0

Views: 110

Answers (1)

Lothar
Lothar

Reputation: 527

Really? That's very confusing behaviour. What are you trying to achieve?

Nevertheless even if you use code like this it's at least better to randomise them in the php script (I am assuming you are using php).

$str_array = array('4_127','2_84','2_85');
shuffle($str_array);
$str = implode(", ",$str_array)

Upvotes: 1

Related Questions