Skip to content
Advertisement

How do I search for a certain attribute in mysql JSON?

I can use this mysql query to search for all rows where attribute ‘color’ is Red

SELECT * FROM table WHERE JSON_EXTRACT(`attribute`,'$.color') = 'Red'

I can use this mysql query to search for all rows where attribute ‘color’ is not Red

SELECT * FROM table WHERE JSON_EXTRACT(`attribute`,'$.color') != 'Red'

But how do I search for all rows where attribute ‘color’ is found regardless of color? And how do I search for all rows where attribute ‘color’ is not found?

Advertisement

Answer

Use next queries:

SELECT * FROM tbl WHERE JSON_EXTRACT(`attribute`,'$.color') IS NULL ;

SELECT * FROM tbl WHERE JSON_EXTRACT(`attribute`,'$.color') IS NOT NULL ;

fiddle

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement