为什么 Rose::DB::Object sort_by RAND() 没有达到我的预期?
我无法让它工作。 我正在使用这个查询:
my $user_questions
= RoseDB::UserSecurityQuestion::Manager->get_user_security_questions(
query => [
'user.username' => $username,
],
with_objects => ['User','SecurityQuestion'],
sort_by => 'RAND()',
limit => 2,
);
当我在 Rose::DB::Object::Manager 中打开调试时,我看到 order 子句是:
ORDER BY t1.id, RAND()
t1.id
来自哪里? 知道如何将 ORDER BY
更正为 RAND()
吗?
I can't get it to work. I'm using this query:
my $user_questions
= RoseDB::UserSecurityQuestion::Manager->get_user_security_questions(
query => [
'user.username' => $username,
],
with_objects => ['User','SecurityQuestion'],
sort_by => 'RAND()',
limit => 2,
);
When I turn on debugging in Rose::DB::Object::Manager, I see that the order clause is:
ORDER BY t1.id, RAND()
Where is that t1.id
coming from? And any idea how I can correct the ORDER BY
to be just RAND()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 < code>sort_by 参数:
为了正确地将子对象与其父对象关联起来,这是必需的。
如果您想覆盖此行为,可以使用(尚未记录的)
no_forced_sort
布尔参数。但是这很可能会导致子对象与不正确的父对象关联。 要完成这项工作,您需要进行一种确定性地基于 t1 的独特特征的排序,但在其他方面是随机的。 也就是说,
somefunc(t1.id)
是随机的,但对于给定的 t1.id 值始终会返回相同的结果,从而使所有子级都具有正确的父级。一个明显的(可能更务实的)方法是获取用户
$username
的所有安全问题,然后随机选择两个:现在您已经得到了(最大)
NUM_RANDOM_QUESTIONS< /code> 在
@questions
中随机选择问题。From the documentation for the
sort_by
parameter:This is required in order to correctly associate sub-objects with their parent objects.
If you'd like to override this behavior, you can use the (as-yet undocumented)
no_forced_sort
boolean parameter.But it is highly likely that this will cause sub-objects to be associated with the incorrect parent objects. What you'll need to make this work is a sort that is deterministically based on a unique characteristic of t1, but is otherwise random. That is,
somefunc(t1.id)
would be random but would always return the same result for a given value of t1.id, keeping all the children with the correct parents.An obvious (and probably a lot more pragmatic) approach is to fetch all security questions for the user
$username
and then just randomly pick two:Now you've got your (maximum)
NUM_RANDOM_QUESTIONS
randomly selected questions in@questions
.