'SQL join by non null values

I have the following table:

Filters

filter_1 filter_2 filter_3
test another value
test NULL NULL
test random random

And I have another like this:

Forms

id name title
test another value
test example NULL

Now the idea is to join both tables by the fields that they match if the filter is not null.

For instance the first filter should match the first form and the second filter should match both forms and the third should match none.

I tried the following:

SELECT COALESCE(filter_1, id), COALESCE(filter_2, name), COALESCE(filter_3, title)
FROM FORM
JOIN FILTER on (id = filter_1 or name = filter_2 or title = filter_3)

Unfortunately this will match if one of the fields match and if I change to and we will match if all fields match.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source