从每种类型中选择随机值

时间:2010-05-18 16:56:30

标签: mysql

我有两张桌子,评分:

+-----------+-----------+-------------+----------+
| rating_id | entity_id | rating_code | position |
+-----------+-----------+-------------+----------+
|         1 |         1 | Quality     |        0 |
|         2 |         1 | Value       |        0 |
|         3 |         1 | Price       |        0 |
+-----------+-----------+-------------+----------+

并且rating_option

+-----------+-----------+------+-------+----------+
| option_id | rating_id | code | value | position |
+-----------+-----------+------+-------+----------+
|         1 |         1 | 1    |     1 |        1 |
|         2 |         1 | 2    |     2 |        2 |
|         3 |         1 | 3    |     3 |        3 |
|         4 |         1 | 4    |     4 |        4 |
|         5 |         1 | 5    |     5 |        5 |
|         6 |         2 | 1    |     1 |        1 |
|         7 |         2 | 2    |     2 |        2 |
|         8 |         2 | 3    |     3 |        3 |
|         9 |         2 | 4    |     4 |        4 |
|        10 |         2 | 5    |     5 |        5 |
|        11 |         3 | 1    |     1 |        1 |
|        12 |         3 | 2    |     2 |        2 |
|        13 |         3 | 3    |     3 |        3 |
|        14 |         3 | 4    |     4 |        4 |
|        15 |         3 | 5    |     5 |        5 |
+-----------+-----------+------+-------+----------+

我需要一个SQL查询(不是应用程序级别,必须保留在数据库中),它将随机选择一组评级。示例结果将如下所示,但会在后续调用中为每个rating_id选择一个随机值:

+-----------+-----------+------+-------+----------+
| option_id | rating_id | code | value | position |
+-----------+-----------+------+-------+----------+
|         1 |         1 | 1    |     1 |        1 |
|         8 |         2 | 3    |     3 |        3 |
|        15 |         3 | 5    |     5 |        5 |
+-----------+-----------+------+-------+----------+

我完全停留在随机部分,到目前为止,通过rating_id进行分组一直是一个废话。任何MySQL忍者想要刺伤吗?

谢谢, 乔


编辑:我已经在一堆组合中尝试了rand(),我确信有必要创建结果的随机性,但我无法弄清楚如何为每个组合返回一个随机行评级中的行。我无法使用order by rand() limit 1,因为我需要三行,order by rand() limit 3不会给我一个rating_id,这是最终目标。我需要rand()和子查询或连接的组合,以便我可以确保每个rating_id之一。

4 个答案:

答案 0 :(得分:2)

好吧,有点乱,但似乎做了这个工作。有人可能知道他们做得比我做得更好,可以清理它:

SELECT random.rating_id, random.rand_option_id, r3.code, r3.value, r3.position
FROM
    (SELECT r.rating_id,
        (SELECT r2.option_id
         FROM rating_option r2
         WHERE r2.rating_id = r.rating_id
         ORDER BY RAND() LIMIT 1) AS 'rand_option_id'
     FROM rating_option r
     GROUP BY r.rating_id
    ) random
    LEFT JOIN rating_option AS r3 ON r3.option_id = rand_option_id

结果(当然每次都不一样):

+-----------+----------------+------+-------+----------+
| rating_id | rand_option_id | code | value | position |
+-----------+----------------+------+-------+----------+
|         1 |              4 |    4 |     4 |        4 |
|         2 |              6 |    1 |     1 |        1 |
|         3 |             13 |    3 |     3 |        3 |
+-----------+----------------+------+-------+----------+

答案 1 :(得分:-1)

您是否查看过rand()函数?

SELECT column FROM table
ORDER BY RAND()
LIMIT 1

http://www.petefreitag.com/item/466.cfm

http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand

示例代码

select *
from rating_option
group by rating_id
order by rand()

答案 2 :(得分:-1)

您可以使用rand()功能在评级表中的选择中进行排序。

例如:

select rating_id from rating order by rand() limit 1

答案 3 :(得分:-1)

正如您的评论和上面的其他帖子所阐明的那样

select * from rating_option order by rand()

将以随机顺序返回所有记录...但是,如果您只想要X号码,那么将其包括为其他人所指出的限制

select * from rating_option order by rand() limit 5 (or whatever number)