Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
-3
rated 0 times [  4] [ 7]  / answers: 1 / hits: 7503  / 4 Years ago, mon, may 4, 2020, 12:00:00

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.


More From » postgresql

 Answers
2

You could do:



await getManager().createQueryBuilder('entity')
.select('column')
.distinct(true)
.getRawMany();

[#3931] Friday, May 1, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
benitoh

Total Points: 150
Total Questions: 113
Total Answers: 104

Location: India
Member since Wed, Aug 26, 2020
4 Years ago
benitoh questions
Sun, Mar 21, 21, 00:00, 3 Years ago
Mon, May 13, 19, 00:00, 5 Years ago
Thu, Apr 4, 19, 00:00, 5 Years ago
;