简体   繁体   English

当聚合 function 为 null 时,Sequelize 得到结果

[英]Sequelize get results when aggregate function is null

I have the following code which returns customers.我有以下代码返回客户。 The problem is that I get results only for customers that have placed an order and have created an address.问题是我只为已下订单并创建地址的客户获得结果。

CustomerModel.findAll({
    attributes: {
      include: [
        [db.sequelize.fn("COUNT", db.sequelize.col("orders.id")), "orderCount"],
        [db.sequelize.literal(`(
          SELECT o.createdDate
          FROM orders AS o
          WHERE
              o.customerId = customer.id
          ORDER BY o.createdDate DESC
          LIMIT 1
      )`),
      'latestOrderDate'],
      [db.sequelize.fn('sum', db.sequelize.col('orders.amount')), 'totalAmount'],
      ]
    },
    include: [
      { model: UserModel, as: 'user' },
      { model: AddressModel, required:false },
      { model: OrderModel, attributes: [], required:false }
    ]
  })

Is it possible to get all customers regardless of having an order or an Address?无论有订单或地址,是否有可能获得所有客户?

Thank you!谢谢!

I ended up changing the two sequelize.fn to literal and it worked like a charm!我最终将两个 sequelize.fn 更改为文字,它就像一个魅力!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM