【发布时间】:2022-03-18 20:17:29
【问题描述】:
我在 typeorm 中设置了以下查询构建器。
const sql = this.attendanceRepository
.createQueryBuilder("attendance")
.leftJoin("attendance.child", "child")
.select("attendance")
.addSelect("CONCAT_WS(' ', child.firstName, child.middleName, child.lastName)", "childName")
.addSelect("child.class")
.where("attendance.id= :id", { id: id})
.getRawOne()
const Result = await sql
该查询构建器产生以下SQL
SELECT `attendance`.`id` AS `attendance_id`, `attendance`.`child_id` AS `attendance_child_id`,
`attendance`.`attendance_cd` AS `attendance_attendance_cd`, `attendance`.`come_home_time` AS `attendance_come_home_time`, `attendance`.`late_time` AS `attendance_late_time`, `attendance`.`memo` AS `attendance_memo`, `attendance`.`created_at` AS `attendance_created_at`,
`attendance`.`updated_at` AS `attendance_updated_at`, `attendance`.`deleted_at` AS `attendance_deleted_at`, `child`.`class` AS `child_class`,
CONCAT_WS(' ', `child`.`first_name`, `child`.`middle_name`, `child`.`last_name`) AS `childName`
FROM `attendances` `attendance`
LEFT JOIN `children` `child`
ON `child`.`id`=`attendance`.`child_id`
WHERE ( `attendance`.`id`= ? )
AND ( `attendance`.`deleted_at` IS NULL )
它返回了以下数据。
{
"attendance_id": 2,
"attendance_child_id": 4,
"attendance_attendance_cd": 3,
"attendance_come_home_time": "11:50:00",
"attendance_late_time": "23:40:00",
"attendance_memo": "test",
"attendance_created_at": "2020-10-16T13:33:00.053Z",
"attendance_updated_at": "2020-10-19T10:34:55.000Z",
"attendance_deleted_at": null,
"child_class": "S",
"childName": "hikaru hikaru"
}
但我想要的结果如下
{
"id": 2,
"child_id": 4,
"attendance_cd": 3,
"come_home_time": "11:50:00",
"late_time": "23:40:00",
"memo": "test",
"created_at": "2020-10-16T13:33:00.053Z",
"updated_at": "2020-10-19T10:34:55.000Z",
"deleted_at": null,
"child_class": "S",
"childName": "hikaru hikaru"
}
如何删除前缀 attendance_ ?
如果有人有意见,请告诉我。
谢谢
【问题讨论】:
-
我不知道 typeorm,但这有帮助吗:stackoverflow.com/questions/56311203/…
-
没有。这没有帮助。