Mysql order by和random不工作

Mysql order by和random不工作,mysql,sql,Mysql,Sql,这是我的桌子 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 这里random不工作,有什么问题吗?试试看 SELECT *,MAX(price-s

这是我的桌子

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 
这里random不工作,有什么问题吗?

试试看

SELECT *,MAX(price-saleprice) as newprice  
FROM `products` 
GROUP BY id 
ORDER BY  newprice desc, rand() LIMIT 3

没有一行共享相同的newprice,因此不需要使用rand()。“按新价格订购”就足够了。

newprice
值为250150650和500。这些都是不同的价值观。我们将按此列对您的结果进行排序,只有到那时,如果您有“平局”,这些结果才会随机排序。

您是否正在寻找类似的结果(随机抓取三种产品,按新价格降序)


这里是演示

您的查询结果是什么,应该是什么?您可能记录太少,所以Rand似乎不起作用。如果我写Rand(),newprice随机出现,但newprice not PROPERTY p 3应该随机出现。前3名newprice是250、150和450,因此,将订购3,1,2这3个随机出现,如3,2,1,3,2,但前3个都有不同的新价格,因此不使用兰德。它们是按纽普赖斯分类的,而纽普赖斯对每一个都是不同的。前三排与第四排有何不同?
SELECT *, (price - saleprice) newprice  
  FROM
(
  SELECT *
    FROM `products` 
   ORDER BY RAND()
   LIMIT 3 
) q
  ORDER BY newprice DESC