I have a table in my application which maps products to categories product_to_category
:
+------------+-------------+
| product_id | category_id |
+------------+-------------+
| 1 | 5 |
| 2 | 4 |
| 3 | 7 |
| 4 | 8 |
| 4 | 1 |
| 5 | 10 |
+------------+-------------+
Now I have to create another table similar to this but with updated category_ids
based on a third mapping table:
new_product_to_category
+------------+-------------+
| product_id | category_id |
+------------+-------------+
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 4 | |
| 5 | |
+------------+-------------+
new_mapping
+----------+---------+
| old_cats | new_cat |
+----------+---------+
| 1,2,3 | 91 |
| 12,11 | 92 |
| 55,23 | 93 |
| 43,78 | 94 |
| 4,5,6 | 95 |
| 33,37,39 | 96 |
+----------+---------+
If I could do a join with IN
clause then I would do something like this:
INSERT INTO new_product_to_category (product_id, category_id)
SELECT pc.product_id, nm.new_cat
FROM product_to_category as pc
JOIN new_mapping as nm
ON pc.category_id IN nm.old_cats
But I guess I can't use IN
clause in JOIN
, how I can achieve the same functionality?