Typeorm子查询添加选择

时间:2019-03-13 07:00:12

标签: typeorm

我是使用typeorm的新手,这是我第二次与typeorm混淆,我有以下查询:

SELECT t1.a,t1.b,t2.a
      (SELECT TOP 1 t1.a
       FROM table1 t1
       WHERE t1.b = t2.a
       ORDER BY t1.a DESC
      ) AS MaxT1
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.a = t2.a

我试试这个:

let query = await getManager()
        .createQueryBuilder(Bid, 'bid')
        .select([
            'l.ID_anv_Lot',
            'l.LotNumber',
            'w.WineryName',
            'bid.BidAmount',
            'bid.ProxyBidAmount',
            'er.ID_Contact'
        ])
        .addSelect(Table1, t1)
        .innerJoin(Lot, 'l', 'l.lotNumber = bid.lotNum AND l.paddleNumber = bid.paddleNumber')

但结果是表1上的所有行

1 个答案:

答案 0 :(得分:0)

此示例可以帮助您执行子查询执行:

const posts = await connection.getRepository(Post)
            .createQueryBuilder("post")
            .where(qb => {
                const subQuery = qb.subQuery()
                    .select("usr.name")
                    .from(User, "usr")
                    .where("usr.registered = :registered")
                    .getQuery();
                return "post.title IN " + subQuery;
            })
            .setParameter("registered", true)
            .orderBy("post.id")
            .getMany();

谢谢