简体   繁体   中英

Best way to join different tables in MySQL based on condition?

I have the following tables:

Licenses :

 License    |    object_type_id  |  selected_object_type_id  
  L1                   1                     300 
  L4                   2                     300
  L5                   3                     127 
  ..                   ..                    ...

object_type

id    |    type  |
1          Main
2          Sub
3          Feature

Main

id   | product  | ....
1        PMA
2        PMB
3        PMC
...       ...
300       PMZZ

Sub

id  | product | ...
1       PSA1
2       PSA2
3       ...
300     PSAZZ

Feature

id  |  Feature|  ...
1        FO1
2        FO2
3        FO3
..        ..
127       FFZ127

I want to have a final table called "license_mapping"

license_mapping

 license   |   license_object | license_object_type
   L1              PMZZ              Main
   L4              PSAZZ             Sub
   L5              FFZ127            Feature

The logic goes that for a given license, find the licenses.object_type_id . Depending on value of object_type.type where licenses.object_type_id = object_type.id choose the product or feature from the corresponding table that matches the licenses.selected_object_type_id .

But how do I handle the case Main and Sub since they have the same id and also the same column name?

If I am to write pseudo SQL statement it would be the following, the last line is where I don't understand how to do it correctly.

 SELECT
   L.License,
   CASE WHEN OT.type = 'Main' THEN M.Product WHEN OT.type = 'Sub' THEN S.Product WHEN OT.Type = 'Feature' THEN F.Feature END AS License_Objects,
   OT.type As license_object_type
 FROM 
   Licenses L
   JOIN object_type OT on L.object_type_id = OT.id
   JOIN Feature F on L.object_type_id = F.id
   JOIN Main M when OT.type = 'Main' and Sub S when OT.type = 'Sub'

You just need to LEFT JOIN to each of the Main , Sub and Feature tables on the selected_object_type_id value and select the appropriate value from those tables based on the object_type :

SELECT l.License, 
       CASE WHEN ot.type = 'Main' THEN m.product 
            WHEN ot.type = 'Sub' THEN s.product
            WHEN ot.type = 'Feature' THEN f.Feature
       END AS license_object,
       ot.type AS license_object_type
FROM licenses l
JOIN object_type ot ON ot.id = l.object_type_id
LEFT JOIN main m ON m.id = l.selected_object_type_id
LEFT JOIN sub s ON s.id = l.selected_object_type_id
LEFT JOIN feature f ON f.id = l.selected_object_type_id
ORDER BY l.License

Output for your sample data:

License     license_object  license_object_type
L1          PMZZ            Main
L4          PSAZZ           Sub
L5          FFZ127          Feature

Demo on dbfiddle

You can use two JOIN s to Main . However, presumably there is also a condition on the id -- and it is not clear what that should be JOIN ed to:

 SELECT L.License,
        (CASE WHEN OT.type = 'Main' THEN MS.Product
              WHEN OT.type = 'Sub' THEN MS.Product
              WHEN OT.Type = 'Feature' THEN F.Feature
         END) AS License_Objects,
        OT.type As license_object_type
FROM Licenses L JOIN
     object_type OT
     ON L.object_type_id = OT.id JOIN
     Feature F 
     ON L.object_type_id = F.id LEFT JOIN
     Main MM
     ON MM.ID = ??? AND OT.type = 'Main' LEFT JOIN
     MAIN MS 
     ON MS.ID = ??? AND OT.type = 'Sub';

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