【问题标题】:How to concat string the Column value in sequelize?如何在续集中连接列值?
【发布时间】:2021-09-19 21:02:17
【问题描述】:

我在下面的代码中,我想将 url 路径连接到图像名称。

let product_list = await Product.findAll({
      limit: limit,
      offset: offset,
      where: {
        isActive: 1
      },
      order: [
        ['createdAt', 'DESC'],
      ],
      include: { 
        model: ProductImage, 
        limit: 1, 
        offset: 0,
        attributes: [
          [db.sequelize.literal("https://abc.s3-us-west-2.amazonaws.com/ || '' || image"), 'image']
        ],
      },
    });

我怎样才能得到想要的输出?

【问题讨论】:

  • 确保将所需的输出添加到问题本身。
  • 图片在哪里?在 db 中还是在 js 中?

标签: mysql node.js express sequelize.js


【解决方案1】:

您的分隔符似乎不匹配。尝试对 javascript 使用双引号,对 SQL 使用单引号。

attributes: [
  ["concat('https://abc.s3-us-west-2.amazonaws.com/',  image)" , 'image']
],

这假定“图像”是 ProductImage 表中的一列。我正在使用concat,因为我们的mySql 实例没有配置为||

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2019-12-01
    • 2016-01-10
    • 1970-01-01
    相关资源
    最近更新 更多