TypeORM 内部连接相同 Table

TypeORM Inner Join Same Table

我有一个问题在网上找不到答案。

我有一个 table,我想删除重复的条目:

我的原始 SQL 查询是:

#Perform delete duplicate logs with same order_id and message
DELETE t1 FROM order_logs t1
INNER  JOIN order_logs t2
WHERE t1.log_id < t2.log_id
AND t1.order_id = t2.order_id
AND t1.message = t2.message
AND t1.order_id = @ORDER_ID
AND t2.order_id = @ORDER_ID;

这是我的@Entity:

@Entity('customer_logs')
export class CustomerLog {
    @PrimaryGeneratedColumn()
    readonly log_id: number

    @Column()
    @ManyToOne(() => Customer, (customer) => customer.id, {
        onDelete: 'CASCADE',
    })
    @JoinColumn({ name: 'customer_id' })
    readonly customer_id: number

    @Column({ default: null, nullable: true })
    @ManyToOne(() => User, (user) => user.user_id)
    @JoinColumn({ name: 'user_id' })
    readonly user_id?: number

    @Column()
    @IsString()
    @MinLength(1)
    @MaxLength(255)
    readonly message: string

    @Column()
    @IsEnum(CustomerLogType)
    readonly type: CustomerLogType

    @Column({ type: 'json', default: null, nullable: true })
    @IsObject()
    readonly json?: object

    @CreateDateColumn()
    @Expose({ groups: ['ADMIN', 'OWNER'] })
    readonly created_at: Date

    @UpdateDateColumn()
    @Expose({ groups: ['ADMIN', 'OWNER'] })
    readonly updated_at: Date

    constructor(partial: Partial<CustomerLog>) {
        Object.assign(this, partial)
    }
}

您知道我将如何通过 QueryBuilder 使用 TypeORM 执行此查询吗?

如果您已经拥有原始 SQL 查询权限,您可以直接在代码中使用它,而无需使用 QueryBuilder 构建它。

import { getConnection } from "typeorm";
.......{

await getConnection().query("DELETE t1 FROM order_logs t1
INNER  JOIN order_logs t2
WHERE t1.log_id < t2.log_id
AND t1.order_id = t2.order_id
AND t1.message = t2.message
AND t1.order_id = @ORDER_ID
AND t2.order_id = @ORDER_ID;"
    );

}