- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 knex.js 构建排序查询。我想加入外部表条目的计数,并按此加入的表计数对结果进行排序。
到目前为止,我已经想出了这段代码:
async eventsPaginationB2C(limit, after, filter, sort) {
const queryBuilder = knex
.select(
'e.id as id',
'e.title as title',
'e.description as description',
'e.startDate as startDate',
'e.endDate as endDate',
'e.userId as userId',
'e.username as username',
'e.status as status',
'e.lang as lang',
'e.online as online',
'e.range as range',
'e.projectName as projectName',
'e.repeatingEvent as repeatingEvent',
'e.ranking as ranking'
)
.limit(limit)
.offset(after)
.from('event as e')
.where(function() {
this.where('status', 'live');
})
.where(function() {
this.where('range', 'global');
});
if(sort){
if (hasIn(sort, 'likes')) {
const desc = sort.likes.desc === true ? 'desc' : 'asc';
queryBuilder
.join(knex.raw(`(SELECT *, COUNT(likes.user_id) AS Count FROM likes WHERE id = likes.event_id )`))
.orderBy('Count', desc);
}
}
return queryBuilder;
}
此查询显示结果,但它们未按“喜欢”排序。此外,将排序器值从 true 更改为 false 不会更改顺序。
最佳答案
您没有正确引用作为Count
,因此order by "Count"
将不匹配任何列。
这可能有帮助:
knex.raw(`(SELECT *, COUNT(likes.user_id) AS ?? FROM likes WHERE id = likes.event_id )`, ['Count'])
关于javascript - 如何在 knex.js 中加入外部表条目的计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57286919/
knex.raw(sql, bindings)和 knex.schema.raw(statement) . 似乎这两个函数具有不同的签名。 如果它们相等,我该如何使用 knex.schema.raw(
我有一些 Knex 迁移脚本,如下所示: 'use strict'; exports.up = function (knex) { return knex.schema .ha
我希望基本上在 Knex 中进行此类查询,但我无法使其正常工作: select distinct * from ( select *, 1 as rank from table1 where Wo
我想为 Knex 迁移中的主键设置默认值,但我遇到了问题。这是我最初的迁移: exports.up = (knex, Promise) => { return knex.schema.create
我发现有两种方法可以在迁移文件中编写 knex 迁移。 exports.up = function (knex) { return knex.schema .createTable
我正在使用 MSSQL,我有一个存储过程返回多个结果集(1 个用于数据,1 个用于记录计数)当我使用 knex.raw 调用此存储过程时,Knex 仅返回第一个结果集。如何使用 Knex + Node
我有一个表,当向其中添加新行时,所有现有行都需要更新。本质上,在一个“插入”完成之前,我不想让另一个“插入”发生。 所以我猜普通的行级锁或读锁是不够的,想在事务期间直接锁表。 有没有办法在 Knex
我正在尝试使用以下代码在迁移脚本中将列数据类型从日期修改为时间戳 knex.schema.alterTable('user', function(t) { t.timestamp('bifthda
我的 Knex 驱动程序出现间歇性错误: TimeoutError: Knex: 获取连接超时。游泳池可能已经满了。你错过了一个 .transacting(trx) 调用吗? 我猜,要么是我的 RDS
我有以下路线,我想获取与从第一个查询获得的 id 关联的总和 app.post('/VerEmpresas', function (req, res) { var r, dot, ide; k
在How to return a plain value from a Knex / Postgresql query? ,我询问了如何从 Knex 查询中获取列值,而没有包含列名的 JSON 对象。
我对 knex(和 node js)很陌生。如果存在,我需要编写插入,但我无法执行此操作。 下面不起作用,但有没有办法做到这一点? var val = "water"; knex('ingredien
有没有办法使用 Kenx 将 IF 语句添加到 SELECT 查询?假设我要执行以下查询: SELECT id, name, IF(grade<60,'Fail','Pass') AS examRes
我有一个关于 SQL 连接池的问题。我的团队正在我们的 Node 应用程序之一中使用 knex.js 库来进行数据库查询。应用程序不时需要切换数据库。因此,我的团队创建了一个初始化函数,该函数返回配置
我刚刚接触 knex,遇到了事务。我认为它很有用,因为它具有回滚功能。尝试使用它(参见下面的代码) await knex.transaction(trx => { knex.raw(delete fr
我正在为 Mysql 使用 Knex.js。我运行迁移并成功创建表。之后,当我通过此命令命令 knex seed:run 运行种子时,出现以下错误: Knex:warning - calling kn
我通过以下方式将 order by 绑定(bind)添加到 knex 原始查询。 -- Here userIds = [1,2,3] and dataOrder='DESC' knex.raw.que
尝试针对我的远程 postgres 数据库(不是本地主机)运行 knex seed:run 时出现以下错误:Knex:Error Pool2 - Error: connect ECONNREFUSED
我要在一个函数中对数据库执行五 (5) 项操作。我想将它们放入事务中,但我发现将每个都放入前一个操作的回调中有点奇怪。我怎样才能以优雅和可读的方式做到这一点? 最佳答案 Knex 具有内置的事务支持,
我正在尝试使用 Knex.js 查询生成器构建数据库迁移,但是我收到以下与外键关系相关的错误: Knex:warning - migrations failed with error: alter t
我是一名优秀的程序员,十分优秀!