2

I need to order by the name from the random results. My sql command:

SELECT * FROM oc_manufacturer ORDER BY RAND() ASC LIMIT 0,50

And it returns thhis array:

   [0] => Array
        (
            [manufacturer_id] => 16
            [name] => Azzaro
        )

    [1] => Array
        (
            [manufacturer_id] => 71
            [name] => Sony
        )

    [2] => Array
        (
            [manufacturer_id] => 104
            [name] => Casio
        )

    [3] => Array
        (
            [manufacturer_id] => 30
            [name] => Jeepers Peepers
        )

But i want to return first Azzaro, then Casio, then Jeepers Peepers, and Sony. I need to sort results by name from RAMDOM results. I tried sort arrays and some mysql commands but it is not worked.

2
  • 1
    You can use php's array sorting functions, or just wrap your query with another. E.g.: SELECT * FROM (SELECT * FROM oc_manufacturer ORDER BY RAND() LIMIT 50)x ORDER BY name; Commented Jun 5, 2014 at 10:25
  • I tried array sorting ksort krsort asort arsort but I can't get good result Commented Jun 5, 2014 at 10:25

1 Answer 1

2

You can either

make a subquery (see Strawberry comment)

SELECT * FROM (SELECT * FROM oc_manufacturer ORDER BY RAND() LIMIT 50)x ORDER BY name;

sort the array in PHP

$array = ....; // make query
usort($array, function($item1, item2) {
    return strcmp($item1['name'], $item2['name']);
});
Sign up to request clarification or add additional context in comments.

Comments

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.