如何在Sequelize中自定义GROUP_CONCAT分隔符?

huangapple go评论71阅读模式
英文:

How to Customize GROUP_CONCAT Separator in Sequelize?

问题

要将逗号(分隔符)更改为另一个符号(例如:破折号箭头 -> 符号作为分隔符)在SEQUELIZE中,您可以使用sequelize.literal()和sequelize.fn()来创建自定义查询表达式。以下是您预期的查询的代码:

groupAccess.findAll({
  attributes: [
    'id',
    'group_name',
    'description',
    [sequelize.fn('REPLACE', sequelize.fn('GROUP_CONCAT', sequelize.col('module_name')), ',', '->'), 'module_name']
  ],
  group: ['group_name']
})

这将使用sequelize.literal()函数将逗号替换为箭头符号 ->,以获得预期的查询结果。

英文:

Assuming I have this seqeulize code bellow

groupAccess.findAll({
  attributes: [
    'id',
    'group_name',
    'description',
    [sequelize.fn('GROUP_CONCAT', sequelize.col('module_name')), 'module_name']
  ],
  group: ['group_name']
})

The result of it's query will be like

id  |  group_name  |  description  |  module_name
----+--------------+---------------+---------------------------
1   |  A           |  desc A       |  mod_1,mod_2,mod_3
4   |  B           |  desc B       |  mod_4,mod_5,mod_6,mod_7
8   |  C           |  desc C       |  mod_8,mod_9,mod_10
11  |  D           |  desc D       |  mod_11,mod_12

and sequelize.js res will be like:

[
  {
    id: "1",
    group_name: "A",
    description: "desc A",
    module_name: "mod_1,mod_2,mod_3"
  },
  {
    id: "4",
    group_name: "B",
    description: "desc B",
    module_name: "mod_4,mod_5,mod_6,mod_7"
  },
  {
    id: "8",
    group_name: "C",
    description: "desc C",
    module_name: "mod_8,mod_9,mod_10"
  },
  {
    id: "11",
    group_name: "D",
    description: "desc D",
    module_name: "mod_11,mod_12"
  },
]

Problem: How to change the commas (separators) to another symbol as the separator (in e.g: dash-arrow -> symbol as the separator) IN SEQUELIZE?

Expected query:

id  |  group_name  |  description  |  module_name
----+--------------+---------------+---------------------------
1   |  A           |  desc A       |  mod_1->mod_2->mod_3
4   |  B           |  desc B       |  mod_4->mod_5->mod_6->mod_7
8   |  C           |  desc C       |  mod_8->mod_9->mod_10
11  |  D           |  desc D       |  mod_11->mod_12

Expected res of sequelize.js:

[
  {
    id: "1",
    group_name: "A",
    description: "desc A",
    module_name: "mod_1->mod_2->mod_3"
  },
  {
    id: "4",
    group_name: "B",
    description: "desc B",
    module_name: "mod_4->mod_5->mod_6->mod_7"
  },
  {
    id: "8",
    group_name: "C",
    description: "desc C",
    module_name: "mod_8->mod_9->mod_10"
  },
  {
    id: "11",
    group_name: "D",
    description: "desc D",
    module_name: "mod_11->mod_12"
  },
]

答案1

得分: 1

我在GitHub上找到了这个(https://github.com/sequelize/sequelize/issues/3390#issuecomment-84649641); 不确定是否百分之百有效(因为我不熟悉sequelize),但你可以尝试:

groupAccess.findAll({
  attributes: [
    'id',
    'group_name',
    'description',
    [sequelize.fn('GROUP_CONCAT', sequelize.literal('module_name SEPARATOR "->"')), 'module_name']
  ],
  group: ['group_name']
})
英文:

Found this on GitHub (https://github.com/sequelize/sequelize/issues/3390#issuecomment-84649641); not 100% sure if this works (since I'm not familiar with sequelize), but you can try:

groupAccess.findAll({
  attributes: [
    'id',
    'group_name',
    'description',
    [sequelize.fn('GROUP_CONCAT', sequelize.literal(`module_name SEPARATOR '->'`)), 'module_name']
  ],
  group: ['group_name']
})

huangapple
  • 本文由 发表于 2023年1月6日 05:56:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75024775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定