Open1
【typeorm】from句内でサブクエリする方法
こんな感じのサブクエリがしたい場合
SELECT * FROM (SELECT ~~~~~~ )
typeormで記述↓
getConnection()
.createQueryBuilder()
.select('COUNT(i.id)', 'count')
//以下サブクエリ
.from((sub) => {
return sub
.subQuery()
.select('hoge.id', 'id')
.from(Hoge, 'hoge')
}, 'i')
.getRawMany();
typeormのfrom句内でサブクエリする際のポイント
- connectする段階でrepositoryを指定しない。
- typeormではconnectしたrepositoryが自動的にfromの対象になるので、サブクエリしたい場合はcreateQueryBuilderだけで起動する必要がある。
- subQuery()以下からサブクエリを記述する