2013-11-25 90 views
1

我有我運行一個SQL查詢是:MySQL的排序郵政編碼,然後在每個郵政編碼隨機

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中處理這個。

回答

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() 
+0

哦,不錯...我認爲,如果你以後,它抵消了其他參數的另一個排序參數使用RAND()。我會放棄這一點。 –

+0

我不這麼認爲,它的行爲應該完全相同,就好像在表格中有一個名爲「rand」的列一樣,該列中預先填充了0和<1之間的隨機值。爲了獲得額外的樂趣,您可以在數據庫中添加一個「權重」列,並將其乘以蘭德以將結果加權到特定的行。 'ORDER BY business_zip,RAND()* weight' –