我有这个代码
<?php
function Random($numchar)
{
$categories = "83 76 168 102";
$array=explode(" ",$categories);
shuffle($array);
$newstring = implode($array,",");
return substr($newstring, 0, $numchar);
}
$numbers = Random(216);
$res = str_replace(",","','",$numbers);
$cats = '\''.$res.'\'';
$news = mysql_query("SELECT
item.id AS itemid,
item.cid AS itemcid,
SUBSTRING_INDEX(item.url, '/', 3) AS itemurl,
item.title AS itemtitle,
item.top AS itemtop,
images.id AS imgid,
images.img AS imagesimg,
images.item_id AS imagesitemid,
folders.id AS foldersid,
channels.id AS channelsid,
channels.parent AS channelsparent
FROM item, images, folders, channels
WHERE
item.id = images.item_id
AND item.cid = channels.id
AND channels.parent = folders.id
AND channels.parent = '$cat'
AND FIELD(item.cid, $cats)
ORDER BY
itemtop DESC, RAND(), images.item_id DESC
LIMIT 20");
?>
每次刷新页面时,代码都会从变量 $categories 中定义的类别之一返回 20 个项目。我只想显示每个类别的 5 个项目。是否可以按照我的意图使用单个查询?我想腾出空间,以便可以平等地显示所有类别的项目。
有没有其他方法可以优化查询?