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

Verificare se una colonna JSON MySQL contiene un array vuoto

Ecco due approcci testati su MySQL 5.7.24:

SELECT config FROM mytable WHERE JSON_CONTAINS(config, CAST('[]' AS JSON), '$.tier');

Puoi anche usare JSON_CONTAINS_PATH, oppure una condizione più stretta che controlla sia la presenza di un array vuoto sia la lunghezza zero:

SELECT config FROM mytable WHERE JSON_CONTAINS(config, JSON_ARRAY(), '$.tier') AND JSON_LENGTH(config, '$.tier') = 0;

Senza json_length, anche un array non vuoto può risultare corrispondente, perché contiene comunque un array vuoto.