无法使用Leftjoin从2个表中获取所有数据

时间:2019-05-29 08:22:40

标签: mysql nestjs typeorm

我无法从查询中获取所有列,我只获得了1个表的结果,我还期望有其他表,这里我有2个表a)registration_types和b)packages。我得到了registration_types的结果,但是没有得到包表

   return await createQueryBuilder("registration_types")
            .leftJoinAndSelect("packages", "pk", "pk.registration_type_id = registration_types.id")
            .getMany();

我得到了结果,但不是所有的列

2 个答案:

答案 0 :(得分:0)

嘿,您应该尝试以下查询:

return await createQueryBuilder("registration_types")
    .leftJoinAndSelect("registration_types.packages", Package, "pk", "pk.registration_type_id = registration_types.id")
    .getMany();

OR

return await createQueryBuilder("registration_types")
    .leftJoinAndMapMany("registration_types.packages", Package, "pk", "pk.registration_type_id = registration_types.id")
    .getMany();

答案 1 :(得分:0)

嘿,您应该尝试以下查询:

return await getManager().createQueryBuilder(registrationTypesEntity, "registration_types")
    .leftJoinAndSelect("registration_types.packages", PackageEntity, "pk", "pk.registration_type_id = registration_types.id")
    .getMany();

OR:

return await getManager().createQueryBuilder(registrationTypesEntity, "registration_types")
    .leftJoinAndSelect("registration_types.packages")
    .getMany();