Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typeorm select max with specific column

I want to select maximum value from a table's column named quotationVersion from table quotation,

Code

  getOneMaximumQuotationVersion() {
    const query = this.createQueryBuilder("quotation");
    query.select("MAX(quotation.quotationVersion)", "max");
    // query.addSelect("MAX(quotation.quotationVersion)", "max");
    return query.getOne();
  }
like image 517
DMS-KH Avatar asked Jan 01 '23 10:01

DMS-KH


1 Answers

If you want to add functions like MAX, SUM in the selection clause, you need to do getRawOne() or getRawMany(). This will give you the raw response:

getOneMaximumQuotationVersion() {
    const query = this.createQueryBuilder("quotation");
    query.select("MAX(quotation.quotationVersion)", "max");
    // query.addSelect("MAX(quotation.quotationVersion)", "max");
    return query.getRawOne();
  }
like image 184
Hussain Ali Akbar Avatar answered Feb 07 '23 17:02

Hussain Ali Akbar