mysql php随机结果按名称排序

时间:2014-06-05 10:22:10

标签: php mysql sorting

我需要从随机结果中按名称排序。我的sql命令:

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

它返回这个数组:

   [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
        )

但我想首先回归Azzaro,然后是Casio,然后是Jeepers Peepers和索尼。我需要从RAMDOM结果中按名称对结果进行排序。我尝试了排序数组和一些mysql命令,但它没有用。

1 个答案:

答案 0 :(得分:2)

你可以

制作子查询(参见草莓评论)

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

在PHP中对数组进行排序

$array = ....; // make query
usort($array, function($item1, item2) {
    return strcmp($item1['name'], $item2['name']);
});