Я пытаюсь выполнить следующий запрос:
select *
from A
where id in (
(select id from A where created_at >= '2018-12-25' order by created_at asc limit 1)
union
(select id from A where created_at < '2018-12-26' order by created_at desc limit 1)
)
То, что я пробовал с knex, было:
knex.select('*')
.from('A')
.whereIn('id', qb =>
qb.select('*')
.from('A')
.where('created_at', '>=', '2018-12-25')
.orderBy('created_at', 'ASC')
.limit(1)
.union(qb =>
qb.select('*')
.from('A')
.where('created_at', '<', '2018-12-26')
.orderBy('created_at', 'DESC')
.limit(1)
)
)
Но это приводит к другому SQL:
select *
from "A"
where "id" in (select * from "A" where "created_at" >= ? union select * from "A" where "created_at" < ? order by "created_at" DESC limit ? order by "created_at" ASC limit ?)
Похоже, что order by
предложения не обрабатываются так, как я хочу, также группы скобок не совпадают.В чем моя ошибка здесь?Как мне сделать это правильно с knex?