I'm trying to select a list of random records from a MySQL-table but with priority towards certain ENUM types. It works fine when I run the following plain SQL-query in a terminal:
我尝试从一个mysql表中选择一个随机记录的列表,但是优先考虑某些枚举类型。当我在终端运行下面的纯sql查询时,它工作得很好:
select * from table_name where expires <= UNIX_TIMESTAMP()*1000
order by enum_type desc, rand() limit 500;
But I get a compilation error from my IDE when writing the following code:
但在编写以下代码时,我的IDE出现了一个编译错误:
private List<FooRecord> getNextRecordsWeighted(Condition condition, int recordLimit) {
final long timeNow = System.currentTimeMillis();
return context.selectFrom(TABLE_NAME).where(TABLE_NAME.EXPIRES.lessOrEqual(timeNow)).
orderBy(TABLE_NAME.ENUM_TYPE.desc(), DSL.rand()).limit(recordLimit).fetch();
}
Now, what my IDE says is that there obviously is no compatible method that I can call for doing this. How would I go about to solve this? Is there a workaround?
现在,我的IDE说的是显然没有兼容的方法可以调用。我该怎么解决这个问题呢?有解决方案吗?
1 个解决方案
#1
3
The problem is with the orderBy()
methods' various overloads. You have:
问题是orderBy()方法的各种重载。你有:
SelectOrderByStep(Field, Field)
- SelectOrderByStep(字段、字段)
SelectOrderByStep(SortField, SortField)
- SelectOrderByStep(SortField SortField)
Your TABLE_NAME.ENUM_TYPE.desc()
is a SortField
, whereas DSL.rand()
is a Field
. In order to make this work, you'll have to make DSL.rand()
a SortField
, by calling: DSL.rand().asc()
.
您的TABLE_NAME.ENUM_TYPE.desc()是一个SortField,而DSL.rand()是一个字段。为了使这个工作有效,您必须通过调用:DSL.rand() ().asc()使其成为SortField。
I recognise that this is somewhat of a flaw in the API, which can probably be fixed in a future version of jOOQ. I have created a GitHub issue for this fix: #3631
我认识到,这在某种程度上是API的一个缺陷,可能可以在jOOQ的未来版本中修复。我为这个修复创建了一个GitHub问题:#3631
#1
3
The problem is with the orderBy()
methods' various overloads. You have:
问题是orderBy()方法的各种重载。你有:
SelectOrderByStep(Field, Field)
- SelectOrderByStep(字段、字段)
SelectOrderByStep(SortField, SortField)
- SelectOrderByStep(SortField SortField)
Your TABLE_NAME.ENUM_TYPE.desc()
is a SortField
, whereas DSL.rand()
is a Field
. In order to make this work, you'll have to make DSL.rand()
a SortField
, by calling: DSL.rand().asc()
.
您的TABLE_NAME.ENUM_TYPE.desc()是一个SortField,而DSL.rand()是一个字段。为了使这个工作有效,您必须通过调用:DSL.rand() ().asc()使其成为SortField。
I recognise that this is somewhat of a flaw in the API, which can probably be fixed in a future version of jOOQ. I have created a GitHub issue for this fix: #3631
我认识到,这在某种程度上是API的一个缺陷,可能可以在jOOQ的未来版本中修复。我为这个修复创建了一个GitHub问题:#3631