Where clause op JSON data in array

Pagina: 1
Acties:

Onderwerpen

Vraag


Acties:
  • 0 Henk 'm!

  • L1NO_
  • Registratie: Mei 2013
  • Laatst online: 09-09 17:17
Ik ben al even opzoek naar een manier om een WHERE clause te maken op een JSON array in SQL. (mariaDB)
Met een simpele LIKE kom ik enigszins uit de voeten, zoals

{ "id" : [1, 2, 3] }
----
WHERE "tags LIKE '%\"1\"%' "


Maar ik heb ook kolommen met de volgende layout JSON,

{ "collection" : ["1"], "identity" : ["1"] } >> ?
----

De MEMBER OF functie werkt niet op MariaDB, zou een goede oplossing zijn.

En dit lukt me niet, kan iemand me in de goede richting nudgen?
Probleem beter beschreven, hier.

Alle reacties


Acties:
  • +1 Henk 'm!

Verwijderd

Hier staat beschreven wat er met json kan in MariaDb https://mariadb.com/resources/blog/using-json-in-mariadb/

Acties:
  • +1 Henk 'm!

  • Koptelefoontje
  • Registratie: Mei 2012
  • Laatst online: 07:55
Typisch iets wat je aan chatgpt kan vragen :)

Acties:
  • +1 Henk 'm!

  • L1NO_
  • Registratie: Mei 2013
  • Laatst online: 09-09 17:17
Ik vergeet steeds dat deze tools nog bestaan, de knipklare oplossing van ChatGPT;

code:
1
2
SELECT * FROM my_table
WHERE JSON_SEARCH(my_json_column, 'one', 'some_value', NULL, '$.my_key') IS NOT NULL;


ChatGPT — "In this example, my_table is the name of the table where your JSON data is stored, my_json_column is the name of the column that contains the JSON data, some_value is the value you're looking for within the JSON object, and my_key is the name of the key you want to search within.

The fourth argument to JSON_SEARCH() is the path option, which is set to '$.my_key' to search only within the my_key key of the JSON object.

The JSON_SEARCH() function returns the path of the matched value in the JSON document, or NULL if the value is not found. In the WHERE clause of the query, we check if the result of JSON_SEARCH() is not NULL, indicating that the value was found in the specified key of the JSON object."