0

I have the following Car's table in DB2:

 id     reference
 1      31943149-blue
 2      40213982-red
 3      93713946-blue
 ...

Then I need to find all entries which reference ending is inside a dynamic list of colors. How could I do that? In fact, the dynamic list of colors should be found from another query to another table, so I guess than an UNION or INNER JOIN could be better in terms of performance.

Thanks in advance

8
  • 1
    WHERE reference LIKE '%blue' OR reference LIKE '%red' OR .... . Prepare the query dynamically using the application language code (eg: PHP) Commented Sep 3, 2019 at 5:59
  • But the array of colors is dynamic, I don't know exactly which ones will be Commented Sep 3, 2019 at 6:00
  • That is why I said prepare the query string dynamically. Eg: use foreach .. loop Commented Sep 3, 2019 at 6:01
  • But I could have a very large list of colors, how would be the performance with lots of LIKE OR ? Commented Sep 3, 2019 at 6:01
  • 1
    You can use REGEXP_LIKE with the regexp red|blue|green. You can construct this regexp dynamically from the list of colors. Commented Sep 3, 2019 at 6:07

1 Answer 1

2

You can join the two tables, using LIKE in the ON condition.

SELECT car.*
FROM car
JOIN colors ON car.reference LIKE ('%-' CONCAT colors.color)
Sign up to request clarification or add additional context in comments.

Comments

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.