1

刚收到一个简单的问题,这是通过 PHP 做的一件简单的事情,但我很确定必须有一种方法可以通过 MySQL 来做。

我有一个表格,其中包含 id、element_id、video_link、views 等列。

我想做的事情是从前 5 个观看次数最多的视频中随机选择一个。

例如

SELECT *
  FROM table_name
 WHERE "In human terms order by views descending and then select 1 of the top 5 at random"

通过 PHP 实现非常简单,如果无法通过 MySQL 实现,我将使用它,但很高兴知道!

4

3 回答 3

2
SELECT *
FROM (
    SELECT *
    FROM `tablename`
    ORDER BY id DESC
    LIMIT 5
) AS inner_table
ORDER BY RAND( ) 
于 2012-10-18T06:33:40.327 回答
1
SELECT * FROM 
  (  SELECT * FROM table_name 
      WHERE name  = 'aas' 
     ORDER BY views DESC LIMIT 5 
  )
ORDER BY RAND() LIMIT 1
于 2012-10-18T06:39:32.833 回答
-1

试试这样:

SELECT 
   * 
FROM 
   (SELECT * FROM table WHERE <order by something> LIMIT 0,5) 
ORDER BY 
   RAND() 
LIMIT 0,1
于 2012-10-18T06:29:17.017 回答