sql-server - 将嵌套数组转换为嵌套对象
问题描述
我正在使用 typeorm 从 DB 获取数据,我想列出我的所有问题以及每个问题的最后状态。
我的数据库有 2 个表:Issue 和 IssueStatus 到 IssueStatus 我将保存每个状态更改和有关如何解决问题的评论。
我的代码:
var issues = await issuesRepository
.createQueryBuilder('issue')
.select(['issue', 'history.status'])
.innerJoin('issue.history', 'history')
.leftJoin(
'issue.history',
'history2',
'history.date < history2.date OR (history.date = history2.date AND history.id < history2.id)'
)
.where('history2.id IS NULL')
.getMany();
我得到这个结果:
{
"id": "5ff86c81-a202-4211-84f4-afe2d5c0fc0d",
"cod": 1,
"opendate": "2020-12-08T13:18:55.683Z",
"closedate": null,
"type": "systems",
"subtype": "avance",
"description": "first test",
"mailto": "sustentacao@bonnjur.com.br",
"hostname": "dskcwbti02",
"username": "glenn.veloso",
"solution": null,
"supportby": null,
"history": [
{
"status": 0
}
]
}
但我想要这个:
{
"id": "5ff86c81-a202-4211-84f4-afe2d5c0fc0d",
"cod": 1,
"opendate": "2020-12-08T13:18:55.683Z",
"closedate": null,
"type": "systems",
"subtype": "avance",
"description": "first test",
"mailto": "sustentacao@bonnjur.com.br",
"hostname": "dskcwbti02",
"username": "glenn.veloso",
"solution": null,
"supportby": null,
"status": 0
}
解决方案
你可以像下面那样做 smth,但你需要用别名覆盖所有必填字段select
:
var issues = await issuesRepository
.createQueryBuilder('issue')
.select(['issue', 'history.status AS status']) // <- changes here
.innerJoin('issue.history', 'history')
.leftJoin(
'issue.history',
'history2',
'history.date < history2.date OR (history.date = history2.date AND history.id < history2.id)'
)
.where('history2.id IS NULL')
.getRawMany(); // <- changes here
我的意思是
您将需要用别名覆盖所有必填字段
select
.select([
'issue.id AS id',
'issue.cod AS cod',
'issue.opendate AS opendate',
...
...
...
'issue.solution AS solution',
'issue.supportby AS supportby',
'history.status AS status'
])
推荐阅读
- python - Git Bash 没有按 python 2.7.18 的顺序处理我的打印语句和 raw_inputs
- intellij-idea - “将控制台输出保存到日志文件”在 IDEA 中不起作用
- sql - 获取以下条目不是+1分钟的所有条目
- dataframe - pyspark 未缩放的值对于精确火花来说太大了
- c# - Webclient 在尝试连接它发布的同一服务器时返回 500 错误
- laravel - 如何在 laravel 范围内使用模型函数?
- angular6 - 当我尝试对角度应用程序执行单元测试时..但是在运行测试时我遇到了错误
- jquery - JQuery Accordion,当你折叠一个手风琴时,它们都会折叠。我希望它只会折叠目标手风琴
- python - 当QtableView单元格失去对表单关闭的关注时如何保存正在编辑的数据
- java - JUnits ParametrizedTest 与 TestTemplate