简体   繁体   中英

mysql - Connecting three tables and combining different items in one cell

I have this three different tables:

business contains information about different companies.

business
-------------------------------------------------
  id     registry   city_id   primary_activity_id
-------------------------------------------------
  12     24212432         1                     1
 123     43143321         2                     3     
 432     53242123         3                   431

business_activity describes the secondary activities of each company.

business_activity
------------------------------------------------
business_secondary_activities_id     activity_id
------------------------------------------------
                              12             765
                             123             109
                             432               8
                              12             431
                              12               1
                             432               3

activity

activity
-----------------------------------
  id     identifier     description
-----------------------------------
   1           24-2    construction
   3           35-1     consultancy
 431           12-3   manufacturing
 765           23-2     electronics
 109           34-2        software 
   8            1-2          retail

They are connected in the following way:

  • business.id = business_activity.business_secondary_activities_id

  • business.primary_activity_id = activity.id

  • business_activity.activity_id = activity.id

I want to build only one table that looks like this:

business
----------------------------------------------------------------------------------------------------------------------------------------
  id     registry   city_id   primary_activity_id  primary_activity_identifier secondary_activities_ids  secondary_activities_identifier
----------------------------------------------------------------------------------------------------------------------------------------
  12     24212432         1                     1                        24-2                765,431,1                  23-2, 12-3, 24-2
 123     43143321         2                     3                        35-5                      109                              34-2
 432     53242123         3                   431                        12-3                      8,3                         1-2, 35-1
----------------------------------------------------------------------------------------------------------------------------------------

Note that the secondary_activities_id 's and secondary_activity_identifier 's are combined in one cell and separated by commas.

Could someone help?

SELECT b.id, 
       b.registry, 
       b.city_id, 
       b.primary_activity_id, 
       a1.identifier AS primary_activity_identifier,
       GROUP_CONCAT(DISTINCT ba.activity_id ORDER BY ba.activity_id ASC SEPARATOR ', ') 
           AS secondary_activities_ids,
       GROUP_CONCAT(DISTINCT a2.identifier ORDER BY a2.identifier ASC SEPARATOR ', ')
           AS secondary_activities_identifier
FROM business b
     LEFT JOIN activity a1 ON a1.id = b.primary_activity_id
     LEFT JOIN business_activity ba ON ba.business_secondary_activities_id = b.id
     LEFT JOIN activity a2 ON a2.id = ba.activity_id
GROUP BY b.id, b.registry, b.city_id, b.primary_activity_id, a1.identifier
ORDER BY b.id

The FROM clause is fairly straight forward. Your base is the business ID. Each business has a primary business activity and potentially multiple (or none) secondary activities. Therefore a LEFT JOIN associates all the secondary activities, and the activity table is joined twice to decode what those numbers correspond to.

The key in this problem is using the GROUP_CONCAT() function. It is an aggregation function (similar to COUNT or SUM) used in a query with a GROUP BY clause. It takes all the records in that group column and puts them into a single record. In this case, you didn't specify how you wanted them sorted so I chose ascending order. Remember good design practice says those two fields should at least be sorted in the same order to prevent confusing the user about what he or she is looking at.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM