Reputation: 783
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 complete 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.
Upvotes: 1
Views: 10331
Reputation: 1
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
Upvotes: 0
Reputation: 748
You could do:
await getManager().createQueryBuilder('entity')
.select('column')
.distinct(true)
.getRawMany();
Upvotes: 6