Grails GORM从表中返回随机行吗? [英] Grails GORM to return random rows from table?

查看:393
本文介绍了Grails GORM从表中返回随机行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  keywords =关键字
.findAll(来自关键字where locale = '$ locale'order by rand(),[max:20])

假设有成千上万符合上述条件的表中的行。但似乎从表中返回的行不是随机的,而是按行中存储的顺序排列,尽管在返回的20行的上下文中它们是随机的。对于我的应用程序来说,我希望这个查询从表中返回完全随机的行,就像它可以是行ID 203,行ID 3789,行ID 9087,行ID 789等等一样。这可能怎么样?

解决方案

我使用以下样式:

 Keyword.executeQuery('从关键字order by rand()',[max:9])

,它从整个表中返回随机行(我们使用的是MySQL)。



我不确定为什么执行查询会虽然行为与findAll不同。

In my grails application I have:

keywords = Keyword
    .findAll("from Keyword where locale = '$locale' order by rand() ", [max:20])

Assume there are thousands of rows in the table that match the above criteria. But it seems the rows that are returned from the table are not random but in the order the rows are stored in Db although within the context of 20 rows that are returned they are random. For my application to work I want this query to return completely random rows from the table like it could be row id 203 , row id 3789, row id 9087, row id 789, and so on. How is that possible?

解决方案

I use the following style:

Keyword.executeQuery('from Keyword order by rand()', [max: 9])

and it returns random rows from the entire table (we're using MySQL).

I'm not sure why execute query would behave differently from findAll though.

这篇关于Grails GORM从表中返回随机行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆