在MySQL中,RAND()函数用于生成一个随机数。它通常与ORDER BY子句一起使用,以实现结果集的随机排序。
RAND()函数返回一个介于0和1之间的随机浮点数。每次调用RAND()函数时,它都会生成一个不同的随机值。以下是在MySQL查询中如何使用RAND()函数和ORDER BY子句的示例:
return await this.multiCategoryPromptInfo.createQueryBuilder('info')
.leftJoin(
MultiCategoryPromptRelation,
'relation',
`info.id = relation.prompt_id AND relation.category_id = '${categoryId}' and info.flag = '1'`,
)
.select(['info.*'])
.orderBy('RAND()')
.limit(promptCount)
.getRawMany();