'Joining two tables with foreign keys stored in JSON data type
I have two tables which I want to join, the foreign key used to join the "colors" table is stored in JSON (in an array), is this possible in any way? Or will I have to try and figure it out in JavaScript with a loop?
FYI : I am using mysql2 with express and nodejs.
Table 1, Products:
(
product_id INT
product_name VARCHAR
product_colors JSON
product_price FLOAT
)
Table 2, Colors :
(
color_id: INT
color_name VARCHAR
color_description VARCHAR
)
Example of current 'SELECT * FROM products':
[{
product_id: 1,
product_name: 'name of product',
product_colors: [2, 42, 12, 9],
product_price: 12.95
}]
Example of desired output:
[{
product_id: 1,
product_name: 'name of product',
product_colors: [{color_id: 2, color_name: 'Velvet', color_description: 'A reddish-purple color'}, {color_id: 7, color_name: 'Navy', color_description: 'A darker shade of blue similar to the ocean.'}],
product_price: 12.95
}]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
