简体   繁体   English

MySQL-选择顶部后如何选择随机?

[英]MySQL - How to select random after select top?

I have a query like this: 我有这样的查询:

SELECT Id, Name, image, price, view FROM estore.product ORDER BY view DESC LIMIT 9 选择ID,名称,图像,价格,从estore.product进行查看按订单排序DESC LIMIT 9

and I want select random 5 records in that query. 我想在该查询中随机选择5条记录。 I tried but this code doesn't work: 我试过了,但是这段代码不起作用:

SELECT Id, Name, Image, Price, View FROM (
    SELECT Id, Name, Image, Price, View FROM estore.product ORDER BY View DESC LIMIT 9) 
    ORDER BY RAND() LIMIT 5

How can I do? 我能怎么做? Thanks for watching? 谢谢收看

A subquery must be named. 必须命名子查询。 Try: 尝试:

LIMIT 9) as SubQueryAlias ORDER BY RAND() 
        ^^^^^^^^^^^^^^^^^^

You may want to go and read this thread Multiple rows alternative for RAND() 您可能要去阅读此线程RAND()的多行替代方法

If your table is quite large (and it probably can end up quite large being a product table) the rand() limit is quite a slow query 如果您的表很大(并且作为一个产品表它可能最终会变得很大),则rand()限制是一个很慢的查询

SELECT Id, Name, Image, Price, View FROM estore.product ORDER BY RAND() LIMIT 5

(将子查询用于顺序并限制相同的选择是疯狂的……)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM