任何人都能发光吗?尝试将related_product_id映射到product_id并列出product_id中的所有值。
mysql> SELECT p.product_id, p.sku, p.category_color, pcr.color_related_id_1, pcr.id_1_color
-> FROM product p
-> LEFT JOIN product_color_related pcr ON p.product_id = pcr.product_id
-> WHERE p.product_id >= '100'
-> LIMIT 10;
+------------+-------------------+----------------+--------------------+------------+
| product_id | sku | category_color | color_related_id_1 | id_1_color |
+------------+-------------------+----------------+--------------------+------------+
| 100 | HL-010817-FMWP-BK | black | 101 | silver |
| 101 | HL-020817-FMWP-BK | silver | 100 | black |
| 102 | HL-030817-FMWP-TL | brown | 100 | black |
| 103 | HL-040817-FMWP-BK | black | 104 | silver |
| 104 | HL-050817-FMWP-BE | silver | 103 | black |
| 105 | HL-060817-FMWP-TL | brown | 103 | black |
| 106 | HL-070817-FMWP-BK | black | 107 | brown |
| 107 | HL-080817-FMWP-TL | brown | 106 | black |
| 108 | HL-090817-FMWP-BE | blue | 106 | black |
| 109 | HL-100817-FMWP-BK | black | 110 | brown |
+------------+-------------------+----------------+--------------------+------------+
所以我想要实现的是为实例添加新列color_related_sku,并将值与product_id匹配。这种情况color_related_id_1 101应该有color_related_sku的HL-020817-FMWP-BK,color_related_id_1 100应该有HL-010817-FMWP-BK,如下图所示
+------------+-------------------+----------------+--------------------+------------+-------------------+
| product_id | sku | category_color | color_related_id_1 | id_1_color | color_related_sku |
+------------+-------------------+----------------+--------------------+------------+-------------------+
| 100 | HL-010817-FMWP-BK | black | 101 | silver | HL-020817-FMWP-BK |
| 101 | HL-020817-FMWP-BK | silver | 100 | black | HL-010817-FMWP-BK |
| 102 | HL-030817-FMWP-TL | brown | 100 | black | HL-010817-FMWP-BK |
| 103 | HL-040817-FMWP-BK | black | 104 | silver | HL-050817-FMWP-BE |
| 104 | HL-050817-FMWP-BE | silver | 103 | black | HL-040817-FMWP-BK |
| 105 | HL-060817-FMWP-TL | brown | 103 | black | HL-040817-FMWP-BK |
| 106 | HL-070817-FMWP-BK | black | 107 | brown | HL-080817-FMWP-TL |
| 107 | HL-080817-FMWP-TL | brown | 106 | black | HL-070817-FMWP-BK |
| 108 | HL-090817-FMWP-BE | blue | 106 | black | HL-070817-FMWP-BK |
| 109 | HL-100817-FMWP-BK | black | 110 | brown | HL-110817-FMWP-BN |
+------------+-------------------+----------------+--------------------+------------+-------------------+
答案 0 :(得分:0)
解决方案归功于Jorge
mysql> SELECT p.product_id, p.sku, p.category_color, pcr.color_related_id_1, pcr.id_1_color, p1.sku
-> FROM neo_product p
-> LEFT JOIN neo_product_color_related pcr ON p.product_id = pcr.product_id
-> LEFT JOIN neo_product p1 ON pcr.color_related_id_1 = p1.product_id
-> WHERE p.product_id >= '100'
-> LIMIT 10;
+------------+-------------------+----------------+--------------------+------------+-------------------+
| product_id | sku | category_color | color_related_id_1 | id_1_color | sku |
+------------+-------------------+----------------+--------------------+------------+-------------------+
| 100 | HL-010817-FMWP-BK | black | 101 | silver | HL-020817-FMWP-BK |
| 101 | HL-020817-FMWP-BK | silver | 100 | black | HL-010817-FMWP-BK |
| 102 | HL-030817-FMWP-TL | brown | 100 | black | HL-010817-FMWP-BK |
| 103 | HL-040817-FMWP-BK | black | 104 | silver | HL-050817-FMWP-BE |
| 104 | HL-050817-FMWP-BE | silver | 103 | black | HL-040817-FMWP-BK |
| 105 | HL-060817-FMWP-TL | brown | 103 | black | HL-040817-FMWP-BK |
| 106 | HL-070817-FMWP-BK | black | 107 | brown | HL-080817-FMWP-TL |
| 107 | HL-080817-FMWP-TL | brown | 106 | black | HL-070817-FMWP-BK |
| 108 | HL-090817-FMWP-BE | blue | 106 | black | HL-070817-FMWP-BK |
| 109 | HL-100817-FMWP-BK | black | 110 | brown | HL-110817-FMWP-BN |
+------------+-------------------+----------------+--------------------+------------+-------------------+