1

How to select duplicate content to column?

TableA

Id  title     prod_cat
--  --------- --------
1   product1  
2   product2 
3   product3

TableB

ID  category_title
--  --------------
1   category1
2   category2
3   category3

TableC

ID prod_id cat_id
-- ------- ------
1  1       1  
2  1       2  
3  2       3  

How to show content like this? If product have multicategory in rows in table show category name in next column?

TableOutput

prod_id  cat_id    cat_id2
-------- --------- ----------
produkt1 category1 category2
produkt2 category3 NULL
produkt3 NULL      NULL
Stavr00
  • 3,219
  • 1
  • 16
  • 28
Miro
  • 11
  • 3
  • 2
    search around for "pivot". Tell us and add a tag of your sql language so we may help more. – George Menoutis Jun 26 '18 at 13:19
  • @GeorgeMenoutis it's MYSQL – Miro Jun 26 '18 at 13:31
  • Are you looking for the result to have dynamic columns (e.g. if there are 6 duplicates they all end up on the same row), or are you only bothered about returning 2 cat_id columns? – Monofuse Jun 26 '18 at 13:44
  • @Monofuse if I have 6 duplicates they show only i one row – Miro Jun 26 '18 at 13:49
  • You could try building a query inside a script. So you've have 1 query which runs through a grabs distinct category_titles and the second while loop would build the columns and grab the values. I'm not aware of any dynamic way to grab an unknown amount of columns in MySql. – Monofuse Jun 26 '18 at 13:55
  • Build Queries: https://stackoverflow.com/questions/999200/is-it-possible-to-execute-a-string-in-mysql Using While loop with MySQL: https://stackoverflow.com/questions/7753337/while-syntax-error-in-mysql – Monofuse Jun 26 '18 at 13:57

0 Answers0