用子字符串续用findAndCountAll并用多列区别

时间:2019-03-31 02:11:30

标签: postgresql sequelize.js

我正在尝试按顺序进行findAndCountAll操作。该计数将基于3个不同列的组合。其中一列具有子字符串操作(资源列)

我的客户表如下,

id, clientId, accountId, resource
1,  4f16,     2b18,      table::res/123
2,  4f16,     2b18,      chair::res/123

因此,我要根据 clientId,accountId resourceId (资源列上 res / 之后的字符串)计数行。因此,使用上述条目,我应该得到 1 作为计数,因为两个列的 clientId,accountId和resourceId 相同。

我简单的postgres查询是

  

选择不同的c。“ accountId”,c。“ clientId”,substring(“ resource”   'res /([[^ $] +)')as“ resourceId”         来自“客户”作为c

我的续集查询现在看起来就像没有不同

 const { rows, count} = await this.client.findAndCountAll({
  where: {
     clientId: clientId!
  },
  attributes: ['clientId', 'accountId',
    [fn('SUBSTRING', '"resource" from res/([^$]+)'), 'resId'],
    [fn('COUNT', col('id')), 'totalResource']],
  group: ['clientId', 'accountId', 'resource'],
   raw: true
});

我不确定如何执行该子字符串部分以及在3列​​上进行区别。通过上面的查询,我得到“ SequelizeDatabaseError:函数pg_catalog.substring(unknown)不存在

1 个答案:

答案 0 :(得分:0)

能够使其与以下代码一起使用:

 const { rows, count} = await this.client.findAndCountAll({
      where: {
         clientId: clientId!
      },
      attributes: [
        fn('DISTINCT', col('clientId'), col('accountId'),  [fn('substring', col('resource') , 'res/([^$]+)'), 'resId']) ],
      group: ['clientId', 'accountId', 'resId'],
       raw: true
    });