返回行随机然后排序依据

时间:2012-06-21 17:08:33

标签: mysql mysqli

我有这个查询通过focus.name ASC命令返回结果。我想通过首先通过rand()对其进行排序然后通过focus.name对其进行排序并将其限制为10来扩展它。

$query = mysqli_query($db,"SELECT * FROM vendor_products WHERE 
vendor_products.focus_id IN (SELECT focus.id FROM focus WHERE
focus.name='".mysqli_real_escape_string($db,$focus_name)."' AND 
mydelete='0' ORDER BY focus.name ASC) AND product_id='".mysqli_real_escape_string($db,$product['id'])."' 
AND mydelete='0' ORDER BY focus_id ASC");

1 个答案:

答案 0 :(得分:2)

做这样的事情

select t1.* 
from table t1, (select id from table order by rand() limit 10) t2
where t1.id = T2.id
order by t1.name

请参阅here