I am working on some SQL query with joins and I want to get the corresponding value from another table which matches with original table columns values.
For example, I have two different tables named Product and Category and they are as follows:
Products:
| id | name | category1 | category2 | category3 |
| -- | ------- | ------------------------- |--------------------------- |
| 1 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
| 2 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
| 3 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
| 4 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
| 5 | T-shirt | Cloths&Accessories~:/ID1 | Cloths~:/ID2 | Shirts~:/ID3|
|----|---------|---------------------------|------------- |-------------|
And Category:
| id | categories |
| -- | -------------------- |
| 1 | Cloths & Accessories |
|----|--------------------- |
| 2 | Cloths |
|----|--------------------- |
| 3 | Shirts |
|----|--------------------- |
The problem is, in table Product there is this Cloths&Accessories~:/ID1 kind of Invalid strings stored under column names category1, category2 and category3 and I want to replace them with valid category names from table Category. If you look at the table Category the id of each category matches with string Cloths&Accessories~:/ID1 having ID1 in it.
For example:
Cloths&Accessories~:/ID1 in this string ID1 is related to id=1 of table Category which is Cloths & Accessories
I have to replace and return all of category1, category2 and category3 columns of table Product with valid categories.
What would be the optimal SQL join query for this?