我正在使用typeorm和MySQL。我的实体存储了不同类型的体育数据,例如足球,田径运动-> 100m,200m,400m。
在get调用中,我要按游戏类型(竞技或橄榄球)分组数据,并希望创建如下所示的嵌套JSON
JSON格式: {
"footBall":[
{"location":"use",gameType:"footbal", "goals":2,"result":"won"},
{"location":"use",gameType:"footbal", "goals":1,"result":"draw"}
],
"athletics":[
{"location":"poland",gameType:"athletics", gameSubType:"100m", "rank":2,"result":"first place"},
{"location":"ireland",gameType:"athletics", gameSubType:"200m", "rank":1,"result":"second place"}
]
}
我的实体类是这样的:
@Entity('competition')
export class CompetitionEntity {
@PrimaryGeneratedColumn()
id:Number
@Column({ type: 'timestamp', default: () => "CURRENT_TIMESTAMP"})
date:Date
@Column()
location:String
@Column()
rank:Number
@Column()
eventType: string
@Column()
minutes: number
@Column()
seconds: number
@Column()
miliseconds: number
@Column()
gameType: string //it can take values like athletics or football or baseball etc
@Column()
gameSubType: string //it can take value like 100m, 200m, relay etc
}
在使用中,我编写了这样的代码来获取比赛表中的所有数据
const qb = await getRepository(CompetitionEntity)
.createQueryBuilder('competition')
const competitions = await qb.getMany();
获取结果表单数据库后,我想按gameType列对数据分组并作为嵌套json发送。
请为此指导我
谢谢大家
答案 0 :(得分:2)
您可以以这种方式遍历结果集并分组结果
$cmd = 'sudo ssh-copy-id -i /home/pi/.ssh/id_rsa.pub user@remote_server';
$reply = shell_exec($cmd);
echo $reply;