Mysql
 SQL >> Database >  >> RDS >> Mysql

Come Memorizzare e Interrogare Dati JSON in MySQL

MySQL supporta un tipo di dati JSON nativo, che ti permette di memorizzare, cercare ed estrarre valori JSON in modo efficiente.

Crea una Colonna JSON

CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, details JSON );

Inserisci JSON

INSERT INTO users(details) VALUES ('{ "name": "Safari", "os": "Mac", "spend": 100 }'), ('{ "name": "Chrome", "os": "Windows", "spend": 150 }');

Interroga i Valori JSON

SELECT id, details->>'$.name' AS name FROM users;

L'operatore -> restituisce un valore JSON, mentre ->> restituisce il valore senza le virgolette circostanti. Puoi usare anche valori JSON in WHERE, GROUP BY e query di aggregazione.