I have two columns from different tables that hold JSON-formatted data. The data stored in both columns are arrays. Example:
users
+----+------------------+
| id | options |
+----+------------------+
| 1 | ["AB","CD","XY"] |
| 2 | ["CD","GH"] |
+----+------------------+
items
+----+-------------+
| id | options |
+----+-------------+
| 10 | ["CD","EF"] |
| 11 | ["GH","XY"] |
| 12 | ["GH"] |
+----+-------------+
I wanted to write a query that returns all the rows from users
which matches a given row from items
, using options
columns to perform the match. The rule is if any value in the array is present in both rows, they are a match. Example: user 1 would match items 10 (because of CD
option) and 11 (because of XY
option); user 2 would match items 10, 11 and 12 because all of them have CD
or GH
.
Looking at MySQL docs I found that JSON_OVERLAPS
does exactly that. However, I'm running MySQL 5.7 and the function is only available starting at 8.0.17. There is also no much talking around this function on the web.
How could I emulate JSON_OVERLAPS
behavior on MySQL 5.7 in a query?
Edit: Unfortunately, upgrading to MySQL 8 is not an option since we run MariaDB on production, which also doesn't have that function.