How to do group_concat in select query in Sequelize?

Refresh

5 weeks ago

Views

1k time

1

I want to do GROUP_CONCAT in select query in sequelize here is my requirement what i want

SELECT `group_name`, `description`, GROUP_CONCAT(`module_name`) FROM `group_access` GROUP BY `group_name`

I want to write this type of query in sequelize.

This is what i tried.

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

But it is showing error Unknown column 'GROUP_CONCAT(module_name)' in 'field list'

How to write GROUP_CONCAT in sequelize. I even searched in there website but not found any solution. any help will be appreciated.

1 answers

3

According to Sequelize documentation, you need to use sequelize.fn to do aggregation: Something like this (not tested):

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

Sequelize Querying.