【发布时间】:2022-10-14 15:02:29
【问题描述】:
我正在创建可以带来 3 个表关系结果的查询
this.invoicesRepo
.createQueryBuilder('invoices')
.innerJoinAndSelect('invoices.client', 'client')
.innerJoinAndSelect('invoices.items', 'items')
.select(['invoices.id', 'client.name'])
.groupBy('invoices.id')
.getMany();
返回这些结果
[
{
"id": 1,
"client": {
"name": "Goku"
}
}
]
但我想要这样的结果
// I WANT RESULTS
[
{
"id": 1,
"client": {
"name": "Gok" // just 3 start letters
}
}
]
我为此目的编写了此查询,但它只是返回 id
this.invoicesRepo
.createQueryBuilder('invoices')
.innerJoinAndSelect('invoices.client', 'client')
.innerJoinAndSelect('invoices.items', 'items')
.select(['invoices.id', 'SUBSTR(client.name,1,3)']) // not working
.groupBy('invoices.id')
.getMany();
如何在 typeorm 和 mysql 中达到较高的结果
所以我可以把它总结成一个简单的问题,我如何在 typeorm 中编写这个查询
select SUBSTR(name,1,3),items.invoiceId from client
inner join invoices on invoices.clientId = client.id
inner join items on items.invoiceId = invoices.id
group by items.invoiceId;
【问题讨论】: