mysql 我怎样用queryBuilder把这些查询写成typeorm?

yruzcnhs  于 2023-02-28  发布在  Mysql
关注(0)|答案(1)|浏览(111)

这是mysql查询,我想要到使用在typeorm

select name,items.invoiceId,CONCAT(DAY(invoices.created_At),' ',
        SUBSTR(MONTHNAME(invoices.created_At),1,3),' ',
        Year(invoices.created_At)) as 'Date' 
         from client
          inner join invoices on invoices.clientId = client.id
          inner join items on items.invoiceId = invoices.id
          group by items.invoiceId;

但是我可以使用这个.repo.query('raw_query');但我想知道如何使用它与查询生成器;
三表客户、发票、项目关系;

ctehm74n

ctehm74n1#

我已经得到答案

使用mysql的concat,sum,avg等函数时,我们应该使用getRawMany()或getRawOne()

const dateqry =
      'CONCAT(SUBSTR(MONTHNAME(invoices.created_at),1,3)," ",DAY(invoices.created_at)," ",YEAR(invoices.created_at))';
    this.invoicesRepo
          .createQueryBuilder('invoices')
          .innerJoinAndSelect('invoices.client', 'client')
          .innerJoin('invoices.items', 'items')
          .select(['invoices.id', 'client.name'])
          .addSelect(dateqry, 'created_at')
          .groupBy('items.invoiceId')
          .orderBy('invoices.created_at', 'ASC')
          .getRawMany();

我想要的输出

[
      {
        "invoices_id": 1,
        "client_name": "Goku",
        "created_at": "Oct 13 2022"
      },
      {
        "invoices_id": 2,
        "client_name": "Khabib",
        "created_at": "Oct 14 2022"
      },
      {
        "invoices_id": 3,
        "client_name": "Goku",
        "created_at": "Oct 14 2022"
      }

]

相关问题