node.js - 按左连接的最后一行过滤查询
问题描述
我有一个返回所有包裹的查询,它看起来像这样:
Parcel.createQueryBuilder('parcel')
.leftJoinAndSelect('parcel.events', 'events')
.orderBy({ 'events.timestamp': 'ASC' })
.getMany()
.then(parcels => this.res.send(parcels))
.catch((err: Error) => this.res.status(500).send({ error: err.message }));
我正在尝试根据连接category
中最后一行的列的值过滤结果events
。
的模型events
如下所示:
@PrimaryGeneratedColumn()
id: number;
@Column()
category: string;
@Column()
title: string;
@Column({ nullable: true })
details?: string;
@Column()
location: string;
@Column({
type: 'timestamp with time zone',
default: () => 'CURRENT_TIMESTAMP',
})
timestamp: string;
@ManyToOne(() => Parcel)
parcel: Parcel;
我尝试了很多东西,但似乎都没有,我最后一次尝试是这样的:
Parcel.createQueryBuilder('parcel')
.leftJoinAndSelect('parcel.events', 'events')
.leftJoinAndSelect(qb => qb.subQuery()
.select(['"parcelId"', 'category', 'timestamp'])
.from(ParcelEvent, 'event')
.orderBy({ timestamp: 'DESC' })
.limit(1), 'lastEvent', '"lastEvent"."parcelId" = parcel.id')
.orderBy({ 'events.timestamp': 'ASC' })
.where('"lastEvent"."category" = :status', { status })
.getMany()
.then(parcels => this.res.send(parcels))
.catch((err: Error) => this.res.status(404).send({ error: err.message }));
一般来说,我对 SQL 也有点陌生,所以这对我解决这个问题并没有太大帮助。
解决方案
推荐阅读
- java - 用java抓取网页并下载视频
- ios - 在 iOS 上执行 Unix/任意命令
- javascript - Blogspot 帖子内容:如何使用 javascript 获取第二张图片 src
- reactjs - 从 PC 浏览器访问 cloud9 上的 react 服务器
- vuex - Nuxtjs /Vuex 不适用于复杂对象
- python - Crispy_forms_tags 在我的更新视图中没有用实例填写表单
- android - 照片被其他应用程序使用的相机拍摄后如何访问
- dart - 在 Flutter 中显示本地图像和服务器图像(带缓存)
- linux - Linux上的git也从另一个存储库上传文件
- reactjs - 如何在使用 create-react-app 创建的文件中导入 Emscripten 生成的 .wasm/js 文件