Home > database >  How can I use ABS() and minus opertator in typerom querybuilder orderBy function?
How can I use ABS() and minus opertator in typerom querybuilder orderBy function?

Time:11-22

I'm trying to order by the closest latitude using this sql query

ORDER BY ABS(a.latitude  - 41.876698387331906) ASC

It works fine,

But in typeorm when I try

.orderBy('ABS(address.latitude - 41.41.876698387331906)', 'ASC')

It tells me that the relation 'ABS(address' doesn't exist. Any idea on this?

CodePudding user response:

try adding the ABS in you select as the following:

.addSelect('ABS(address.latitude - 41.41.876698387331906)', 'absLat')

and then order by absLat

.orderBy('absLat', 'ASC')
  • Related