admin管理员组文章数量:1333442
In my backend using nestjs + typeorm + postgresql I have a CustomRepository and want to repalce some plain sql queries.
This is what I have
const sqlQuery = `SELECT DISTINCT "myColumn" FROM "myTable"`
const sqlRes = await this.query(sqlQuery);
I am trying to get something like this
this.find({select:["myColumn"]}); // note the missing DISTINCT
But this is giving me the plete column but I want just the DISTINCT values.
I found a lot of weird createQueryBuilder().select(DISTINCT "myColumn" FROM "...... etc...
solutions, which are not really giving my any benefit over my working solution.
In my backend using nestjs + typeorm + postgresql I have a CustomRepository and want to repalce some plain sql queries.
This is what I have
const sqlQuery = `SELECT DISTINCT "myColumn" FROM "myTable"`
const sqlRes = await this.query(sqlQuery);
I am trying to get something like this
this.find({select:["myColumn"]}); // note the missing DISTINCT
But this is giving me the plete column but I want just the DISTINCT values.
I found a lot of weird createQueryBuilder().select(DISTINCT "myColumn" FROM "...... etc...
solutions, which are not really giving my any benefit over my working solution.
2 Answers
Reset to default 6You could do:
await getManager().createQueryBuilder('entity')
.select('column')
.distinct(true)
.getRawMany();
For some reason getMany or getCount doesn't work for distinct. As 0xCAP said above:
await getManager().createQueryBuilder('entity')
.select('column')
.distinct(true)
.getRawMany();
If you want to keep other columns (as your question), just use addSelect instead of select
本文标签: javascriptTypeORM Custom Repositoryselect distinct values from specified columnStack Overflow
版权声明:本文标题:javascript - TypeORM Custom Repository, select distinct values from specified column - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742288702a2447398.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论