0

I have two tables products and filters. My products table is like below:-

id    name    color_id    size_id    composition_id
 1    Test      Black     60x60 CM       Cotton
 2    Test2     Red       60X90 CM      Acryllic

My filters table as below:-

id    filter_name
 1      Black
 2      Red
 22     60x60 CM 
 23     60X90 CM
 61     Cotton
 62     Acryllic

My Expected output like below:-

id    name    color_id    size_id    composition_id
 1    Test       1            22           61
 2    Test2      2            23           62

I have tried below query that is working for only color_id. Query is below:-

UPDATE products
INNER JOIN filters ON products.color_id = filters.filter_name
SET products.color_id = filters.id

I want to update all ' color_id,size_id,composition_id' in one update statement. May be we need to use Case Statements. Can anyone help?

1 Answer 1

2

One way you can do this is with a set of correlated subqueries, one for each column that you need to look up. We use a COALESCE so that if the value is not found, we retain the previous value:

UPDATE products p
  SET color_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.color_id), color_id),
      size_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.size_id), size_id),
      composition_id = COALESCE((SELECT id FROM filters WHERE filter_name = p.composition_id), composition_id)

Then you can SELECT * FROM products:

id  name    color_id    size_id composition_id
1   Test    1           22      61
2   Test2   2           23      62

Demo on dbfiddle

You can also achieve the same result with an UPDATE ... JOIN query, which might be more efficient:

UPDATE products p
LEFT JOIN filters f1 ON f1.filter_name = p.color_id
LEFT JOIN filters f2 ON f2.filter_name = p.size_id
LEFT JOIN filters f3 ON f3.filter_name = p.composition_id
SET color_id = COALESCE(f1.id, color_id),
    size_id = COALESCE(f2.id, size_id),
    composition_id = COALESCE(f3.id, composition_id)

Demo on dbfiddle

Sign up to request clarification or add additional context in comments.

8 Comments

Your way is right but what if my 100 records are already updated and i again run this query... above 100 records will be empty .. Hope you understand
@kunal I didn't realise you might run the query multiple times. I've updated it (and the demo) for that situation
actually my client don't want to fill ids in column.. So my client gives csv sheet with names every week so i will update the sql query manually . Hope you understand
@kunal I can't say I blame them - much easier to write the words! But the new queries I have posted should do what you want.
COALESCE looks at each of its arguments, starting from the left, and returns the first one that is not NULL. So for example, in the second query, if there is no match for color_id in filters, f1.id will be NULL and the COALESCE(f1.id, color_id) will return the original value of color_id
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.