Imagine a table which tracks baseball pitchers like so...
+------------+--------------------+-------+
| id | name | secondary_pitch |
+------------+--------------------+-------+
| 13 | Chris Sale | ['Curveball','Slider'] |
| 14 | Justin Verlander | ['Fastball','Changeup'] |
| 15 | CC Sabathia | ['Fastball','Curveball'] |
| 16 | Sonny Grey | ['Slider'] |
| 17 | Aldoris Chapman | [] |
+------------+--------------------+-------+
Notice the secondary_pitch column has a JSON value. So if a pitcher, like Chapman, has no secondary pitch, it will not return null, instead it returns an empty JSON string ('[]').
How then can I get a count of the number of pitchers who have no secondary pitch?
I can't do...
select count(*) from pitchers where secondary_pitch is null
['Curveball','Slider']are invalid JSON so NONE of MySQL's native JSON (likeJSON_LENGTH,JSON_SEARCHorJSON_EXTRACTsaw a couple of answers with it) functions would work...["Curveball","Slider"]is valid JSON then the MySQL native JSON functions should work just fine.