我有两个MYSQL查询如下:
$anchors = mysql_query("SELECT * FROM anchors WHERE site_url = '$site_current' AND site_type = 'slave' LIMIT $links_nr");
//第二个查询有一个offset = $ links_nr
$anchors2 = mysql_query("SELECT * FROM `anchors` WHERE `site_url` = '$site_current' AND `site_type` = 'slave' LIMIT $links_nr,99999");
我想按字母顺序显示排序结果,您如何建议我这样做?
谢谢
答案 0 :(得分:0)
在SQL查询中使用ORDER BY site_url ASC
对结果集进行排序
$anchors2 = mysql_query("SELECT * FROM anchors WHERE site_url = '$site_current' AND site_type = 'slave' LIMIT $links_nr,99999 ORDER BY site_url ASC");
答案 1 :(得分:0)
您是否考虑过将结果保存并共同作为多维数组,然后使用uasort()按字母顺序重新排列数组?过去我使用这种方法使用了合并的数据集,并且它运行得很好。如果您对进一步的研究感兴趣,这里是uasort()函数和链接:
http://www.php.net/manual/en/function.uasort.php
(这与usort之间的区别在于,usort不维护索引 - 这可能会损害您的项目:
<?php
// Comparison function
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
print_r($array);
// Sort and print the resulting array
uasort($array, 'cmp');
print_r($array);
?>
以上示例将输出:
Array
(
[a] => 4
[b] => 8
[c] => -1
[d] => -9
[e] => 2
[f] => 5
[g] => 3
[h] => -4
)
Array
(
[d] => -9
[h] => -4
[c] => -1
[e] => 2
[g] => 3
[a] => 4
[f] => 5
[b] => 8
)