Say I have an Image table with a meta column in JSON:
| Id | Meta |
|---|---|
| 1 | { "size": 80, "effect": "blur" } |
| 2 | { "size": 200, "optimize": true } |
| 3 | { "color": "#abcdef", "ext": ".jpg" } |
And I have a dynamic param of table type like so
| Key | Value |
|---|---|
| size | 200 |
| optimize | true |
How should I write my query to filter the rows in which the Meta column's key-value pairs matched all the values in the param table?
SELECT Id
FROM Image
WHERE (
--?? all keys and values matched the param table
)
This is a type of relational division (with remainder) question, with the extra twist of shredding JSON at the same time.
There are a number of solutions to this type of question. One common solution is to
LEFT JOINthe divisor to the dividend, group it and check for any non-matches:Another solution is to use a double
NOT EXISTS: there are no key/value input pairs which do not have a matchdb<>fiddle
YMMV as to which solution is faster.