0

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.

9
  • Is $str = '4_127','2_84','2_85'; the actual string or you have tree strings which are 4_127, 2_84 and 2_85? Commented Feb 8, 2013 at 14:00
  • It is the actual string. Commented Feb 8, 2013 at 14:02
  • do you have ` '4_127','2_84','2_85'` values in your table??? Commented Feb 8, 2013 at 14:12
  • @jcho360 No, they are the values of concating... property_id catalog_id as shown in the query. 4_127: 4 is property_id, 127 is catalog_id and so on... Commented Feb 8, 2013 at 14:15
  • 1
    what you are asking doesn't make sense in MySQL, you are trying to order by 3 items that don't belong to the Table, I guess it would be better do it with help of another programming language like PHP Commented Feb 8, 2013 at 14:21

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Confusing it is, sure. But that's how projects change :) Yes, if nothing else, i might do it in PHP.
Why do you want that to be handled by MySQL ?
There are quite a few other fields/conditions that depend on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.