TypeORM 子查询
TypeORM subqueries
基于typeORM docs on using subqueries,解释了如何创建子查询。
示例:
const qb = await getRepository(Post).createQueryBuilder("post");
const posts = qb
.where("post.title IN " + qb.subQuery().select("user.name").from(User, "user").where("user.registered = :registered").getQuery())
.setParameter("registered", true)
.getMany();
但是没有 SQL 的等价物。
假设我有包含如下子查询的查询:
SELECT a, TO_CHAR (MAX (jointable.f), 'MON YYYY') as f,
t3.c, t3.d, t1.e
FROM table1 t1
LEFT JOIN table2 t2 ON t2.e = t1.e
JOIN table3 t3 ON t3.d = t2.d
JOIN
(SELECT f, t4.g, t5.e, t6.h
FROM table4 t4
JOIN table5 t5 ON t4.g = t5.g
JOIN table6 t6 ON t6.g = t4.g
AND (t6.i = 2
OR (t6.i = 1 AND j = 1)
)
WHERE t4.k = 4
) jointable ON t1.e = jointable.e
WHERE jointable.h = :h
AND(:d = 3 OR
t3."d" = :d
)
GROUP BY a, t3.c, t3.d, t1.e
ORDER BY a ASC
我应该如何为上面的 SQL 查询使用 typeORM 查询构建器函数?
假设我已经创建了与上面查询中使用的所有 table 相关的实体。
我希望这个答案可以帮助其他人使用 TypeORM 子查询。
const subquery = await getManager()
.createQueryBuilder(table4, 't4')
.select('"t4".f')
.addSelect('"t4".g')
.addSelect('"t5".e')
.addSelect('"t6".h')
.innerJoin(table5, 't5', '"t4".g = "t5".g')
.innerJoin(table6, 't6', '"t6".g = "t4".g')
.where('"t4".k = 4 AND ("t6".i = 2 OR ("t6".i = 1 AND "t6".j = 1))');
model = await getManager()
.createQueryBuilder(table1, 't1')
.select('"t1".a')
.addSelect("TO_CHAR (MAX (jointable.f), 'MON YYYY')", 'f')
.addSelect('"t3".c')
.addSelect('"t3".d')
.addSelect('"t1".e')
.leftJoin('table2', 't2', '"t2".e = "t1".e')
.innerJoin(table3, 't3', '"t3".d = "t2".d')
.innerJoin('('+subquery.getQuery()+')', 'jointable', '"t1".e = jointable.e')
.where('jointable.h = :h AND (:d = 3 OR "t3".d = :d)',
{ h: h, d: d })
.groupBy('"t1".a, "t3".c, "t3".d, "t1".e')
.orderBy('"t1".a', 'ASC')
.getRawMany();
我使用 '('+subquery.getQuery()+')'
获取子查询 select 查询,相当于
(SELECT f, t4.g, t5.e, t6.h ....
......
.... ) jointable ON t1.e = jointable.e
据我了解:
Join
actually is equal to inner join
-
.addSelect
类似于 , in select
使用 select 查询可以获得两种类型的结果
构建器:实体或原始结果。描述你是否想要
作为实体的数据(getOne
和 getMany
)或者它是什么
是(getRawOne
和 getRawMany
)。
基于typeORM docs on using subqueries,解释了如何创建子查询。 示例:
const qb = await getRepository(Post).createQueryBuilder("post");
const posts = qb
.where("post.title IN " + qb.subQuery().select("user.name").from(User, "user").where("user.registered = :registered").getQuery())
.setParameter("registered", true)
.getMany();
但是没有 SQL 的等价物。
假设我有包含如下子查询的查询:
SELECT a, TO_CHAR (MAX (jointable.f), 'MON YYYY') as f,
t3.c, t3.d, t1.e
FROM table1 t1
LEFT JOIN table2 t2 ON t2.e = t1.e
JOIN table3 t3 ON t3.d = t2.d
JOIN
(SELECT f, t4.g, t5.e, t6.h
FROM table4 t4
JOIN table5 t5 ON t4.g = t5.g
JOIN table6 t6 ON t6.g = t4.g
AND (t6.i = 2
OR (t6.i = 1 AND j = 1)
)
WHERE t4.k = 4
) jointable ON t1.e = jointable.e
WHERE jointable.h = :h
AND(:d = 3 OR
t3."d" = :d
)
GROUP BY a, t3.c, t3.d, t1.e
ORDER BY a ASC
我应该如何为上面的 SQL 查询使用 typeORM 查询构建器函数?
假设我已经创建了与上面查询中使用的所有 table 相关的实体。
我希望这个答案可以帮助其他人使用 TypeORM 子查询。
const subquery = await getManager()
.createQueryBuilder(table4, 't4')
.select('"t4".f')
.addSelect('"t4".g')
.addSelect('"t5".e')
.addSelect('"t6".h')
.innerJoin(table5, 't5', '"t4".g = "t5".g')
.innerJoin(table6, 't6', '"t6".g = "t4".g')
.where('"t4".k = 4 AND ("t6".i = 2 OR ("t6".i = 1 AND "t6".j = 1))');
model = await getManager()
.createQueryBuilder(table1, 't1')
.select('"t1".a')
.addSelect("TO_CHAR (MAX (jointable.f), 'MON YYYY')", 'f')
.addSelect('"t3".c')
.addSelect('"t3".d')
.addSelect('"t1".e')
.leftJoin('table2', 't2', '"t2".e = "t1".e')
.innerJoin(table3, 't3', '"t3".d = "t2".d')
.innerJoin('('+subquery.getQuery()+')', 'jointable', '"t1".e = jointable.e')
.where('jointable.h = :h AND (:d = 3 OR "t3".d = :d)',
{ h: h, d: d })
.groupBy('"t1".a, "t3".c, "t3".d, "t1".e')
.orderBy('"t1".a', 'ASC')
.getRawMany();
我使用 '('+subquery.getQuery()+')'
获取子查询 select 查询,相当于
(SELECT f, t4.g, t5.e, t6.h ....
......
.... ) jointable ON t1.e = jointable.e
据我了解:
Join
actually is equal toinner join
.addSelect
类似于, in select
使用 select 查询可以获得两种类型的结果 构建器:实体或原始结果。描述你是否想要 作为实体的数据(
getOne
和getMany
)或者它是什么 是(getRawOne
和getRawMany
)。