使用TYPEORM进行多个JOIN

时间:2019-03-05 08:37:49

标签: typeorm

我是typeorm的新手,也许有人可以解决我的问题。
我有一些查询:

SELECT t1.id,t2.id_2,t3.event,t3.column1,t4.column1 FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.id
INNER JOIN table3 t3 ON t2.event = t3.event
INNER JOIN table4 t4 ON t4.id = t2.id_2 WHERE t3.event = 2019

如何将此查询转换为typeorm

1 个答案:

答案 0 :(得分:0)

让我们一一打破,是吗? 假设您知道那么简单:

await getManager()
        .createQueryBuilder(table1, 't1')
        .addSelect('t1.id', 't1_id')
        .addSelect('t2.id_2', 't2_id_2')
        .addSelect('t3.event', 't3_event')
        .addSelect('t4.column1', 't4_column1') // up to this point: SELECT t1.id,t2.id_2,t3.event,t3.column1,t4.column1 FROM table1 t1
        .innerJoin(table2, 't2', 't1.id = t2.id' //INNER JOIN table2 t2 ON t1.id = t2.id
        .innerJoin(table3, 't3', 't2.event = t3.event' // INNER JOIN table3 t3 ON t2.event = t3.event
        .innerJoin(table4, 't4', 't4.id = t2.id_2' // INNER JOIN table4 t4 ON t4.id = t2.id_2 
        .where('t3.event = 2019') // WHERE t3.event = 2019
        .getRawMany() // depend on what you need really

您可以参考此文件检查您想要什么输出:https://github.com/typeorm/typeorm/blob/master/docs/select-query-builder.md#getting-values-using-querybuilder

您是否希望将数据作为实体(getOnegetMany)还是实体(getRawOnegetRawMany