MySQL按邮政编码排序,然后在每个邮政编码中随机排序

时间:2013-11-25 17:44:34

标签: mysql sorting random

我正在运行的SQL查询是:

SELECT * FROM listings, listings_categories 
    WHERE listings.id = listings_categories.listing_id AND category_id 
    IN (1,3,5,8,7,4,5,2) 
    AND business_zip IN (89101,89102,89103,89104,89105,89106) 
    AND status = '1' GROUP BY listings.id

是否有办法对结果进行排序,以便按照business_zip ASC排序,但是在每个邮政编码中随机化结果,或者我最好在php中处理它。

1 个答案:

答案 0 :(得分:3)

你可以有多种排序..

SELECT * FROM listings, listings_categories 
WHERE listings.id = listings_categories.listing_id AND category_id 
IN (1,3,5,8,7,4,5,2) 
AND business_zip IN (89101,89102,89103,89104,89105,89106) 
AND status = '1' GROUP BY listings.id
ORDER BY business_zip, RAND()