1

I'm new to MySQL and am in need of some help. I have a table where each row represents an item for an order. These items are located in a specific area, as seen below:

Order Number            Area
EEN0103944              305
EEN0103945              305
EEN0103945              305
EEN0103902              104
EEN0103902              111
EEN0103945              123
EEN0103945              005
EEN0103945              002

I'm trying to create a table in MySQL that condenses all the information into one row, making orders the primary key, like below:

Order Number             Area
EEN0103945               305
EEN0103945               104, 111, 123
EEN0103945               005, 002
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Kidneb
  • 11
  • 1

1 Answers1

1

You are looking for group_concat() with the distinct option:

select order_number, group_concat(distinct area separator ', ') as areas
from t
group by order_number
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • ... but pull this as a resultset, Don't create database tables like this. This topic is covered in Chapter 2 (Jaywalking) of Bill Karwin's excellent book "SQL Antipatterns: Avoiding the Pitfalls of Database Programming", available at Amazon and other fine booksellers. (I believe a peek at most of chapter 2 is available in the "look inside" feature on Amazon.) – spencer7593 Mar 22 '18 at 22:40