简体   繁体   English

如何使用group by子句选择随机行?

[英]How to select a random row with a group by clause?

I have the following table 我有下表

SQLFiddle SQLFiddle

What I'm attempting to do is to select three random images but to make sure that no two images have the same object, what I attempted to do is to do a GROUP BY along with an ORDER BY rand() but that is failing as it is always giving me cat1.jpg, dog1.jpg, box1.jpg (All images whose path ends with 1 and not the others) 我试图做的是选择三个随机图像,但为了确保没有两个图像具有相同的对象,我试图做的是做一个GROUP BY和一个ORDER BY rand()但是失败了它总是给我cat1.jpg,dog1.jpg,box1.jpg(所有图片的路径以1结尾而不是其他图片)

The fiddle includes the query I ran and how it is not working. 小提琴包括我跑的查询以及它是如何工作的。

What you need is a Random aggregate function. 你需要的是一个随机聚合函数。 Usually there are no such functions in the current RDBMSs. 通常在当前的RDBMS中没有这样的功能。

Similar question has been asked . 有人问过类似的问题。

So the basic idea is shuffle the elements, then group by, and then for every group just select the first row for every group. 所以基本的想法是将元素混合,然后分组,然后为每个组选择每个组的第一行。 If we modify one of answers provided on the link we get this. 如果我们修改链接上提供的答案之一,我们就会得到这个答案。

select object_id, name, image_path
from
  (SELECT images.image_path AS image_path, objects.id AS object_id, objects.name
  FROM objects LEFT JOIN images ON images.object_id = objects.id
  ORDER BY RAND()) as z
group by z.object_id, z.name

You can't get a random image as MySQL always returns that data based on the time of insert (first come, first serve), ie internal order. 您无法获得随机图像,因为MySQL总是根据插入时间(先到先得)返回该数据,即内部订单。

But you can get a random result using following approach ( fiddle ): 但是您可以使用以下方法( 小提琴 )获得随机结果:

SELECT images.image_path AS image_path, objects.name 
FROM objects 
LEFT JOIN 
 ( 
   SELECT object_id, 
      SUBSTRING_INDEX(GROUP_CONCAT(image_path order by rand()), ',', 1) AS image_path
   FROM images
   GROUP BY object_id
 ) as images
ON images.object_id = objects.id 
GROUP BY objects.name 

If there's a restrictive WHERE-condition on the objects table you might get a better performance when you join first and the GROUP_CONCAT. 如果对象表上存在限制性WHERE条件,则在首次加入GROUP_CONCAT时可能会获得更好的性能。

I think this should do: 我认为这应该做到:

ORDER BY random() LIMIT 1 ORDER BY random()LIMIT 1

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

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