This is a read-only copy of the MariaDB Knowledgebase generated on 2024-11-16. For the latest, interactive version please visit https://mariadb.com/kb/.

Finding Intersection?

Hi,

I have the following 2 objects:

{'a': '1', 'b': '1', 'c': '1', 'd': '1'}

{'b': '1', 'd': '1'}

And I would like to find the intersection between them. I could not find a way to do that. I could only find JSON_CONTAINS which just checks for a value.

I found this (https://stackoverflow.com/questions/44687872/find-intersection-of-mysql-json-objects-or-arrays) on Stackoverflow.com, but it's been like 3 years now. Isn't there going to be a native function for this?

Regards

Answer Answered by Daniel Black in this comment.

The main reason it wasn't implemented from the start is it wasn't part of the JSON SQL standard.

The JSON path language in section 6 rules out intersection as a goal however that doesn't preclude it being a function in section 5.

This also explains why attempting to use any function based on JSON path is very hard to make an intersection. With your sample it might be possible to do something with a JSON_TABLE and a JOIN between them and reform the expression in the result.

This looks like a reasonable request for a function, and I couldn't see this requested on JIRA so I recommend adding it as a MDEV task to implement.

There's no way we can use StackOverflow questions as a task list :-)

Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.