這是我的表,mysql命令通過和隨機不工作
id price saleprice
1 4500 4250
2 5250 5100
3 4850 4200
4 2850 2350
和我的查詢是
SELECT *,(price-saleprice) as newprice
FROM `products`
order by newprice desc, rand() LIMIT 3
這裏隨機不工作,這是什麼問題?
這是我的表,mysql命令通過和隨機不工作
id price saleprice
1 4500 4250
2 5250 5100
3 4850 4200
4 2850 2350
和我的查詢是
SELECT *,(price-saleprice) as newprice
FROM `products`
order by newprice desc, rand() LIMIT 3
這裏隨機不工作,這是什麼問題?
newprice
值是250150650和500這些都是不同的值。您的結果將我們訂購的這列只有到那時,如果你有一個「領帶」,那些將被隨機排序。
嘗試
SELECT *,MAX(price-saleprice) as newprice
FROM `products`
GROUP BY id
ORDER BY newprice desc, rand() LIMIT 3
您是否正在尋找類似的東西(隨機抓取3件商品,並將其按新價格下降)?
SELECT *, (price - saleprice) newprice
FROM
(
SELECT *
FROM `products`
ORDER BY RAND()
LIMIT 3
) q
ORDER BY newprice DESC
這裏是SQLFiddle演示
什麼是你查詢的結果,什麼應該是什麼? –
你可能有過少的記錄,以便出現蘭特到不行。 – Mihai
如果我寫rand(),新價格隨機出現,但新價格不合適 – Prakash