0

I have the following table and i want to generate a view. See the result. How can i achieve that? I tried with group_cat but that doesnt work :(

id product_id cat_id date
1  1          1      2018-05-01
2  1          1      2018-05-02
3  1          1      2018-05-04
4  1          1      2018-05-05
5  1          1      2018-05-06
6  1          1      2018-05-07
4  1          1      2018-05-08
5  1          1      2018-05-09
6  1          1      2018-05-10
7  1          2      2018-05-01
8  1          2      2018-05-02
9  1          2      2018-05-04
10  1          2      2018-05-05
11  1          2      2018-05-06
12  1          2      2018-05-07
13  1          2      2018-05-08
14  1          2      2018-05-09
15  1          2      2018-05-10

Result:

product_id cat_id dates
1          1      2018-05-01,2018-05-02,2018-05-03,2018-05-04,etc comma seperated
1          2      2018-05-01,2018-05-02,2018-05-03,2018-05-04,etc comma seperated

Query:

select
  tmp.product_id, tmp.cat_id, group_concat(tmp.date separator ',') as dates
from xxxx as tmp
group by tmp.cat_id;
Bas
  • 2,330
  • 4
  • 29
  • 68

2 Answers2

2

Your group by should be tmp.product_id, tmp.cat_id

select
  tmp.product_id, tmp.cat_id, group_concat(tmp.date separator ',') as dates
from demo as tmp
group by tmp.product_id, tmp.cat_id

Demo

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
1

Try this:

SELECT product_id, cat_id, GROUP_CONCAT(date SEPARATOR ',') dates FROM `table_name` GROUP BY product_id, cat_id
Manash Kumar
  • 995
  • 6
  • 13