mysql - 检查 MySQL 中的 JSON 值是否为空?

标签 mysql sql json null

想象一下像这样跟踪棒球投手的 table ......

     +------------+--------------------+-------+
     | 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    |    [] |
     +------------+--------------------+-------+

请注意 secondary_pitch 列有一个 JSON 值。因此,如果像查普曼这样的投手没有第二个投球,它不会返回 null,而是返回一个空的 JSON 字符串 ('[]')。

那我怎样才能知道没有副投手的投手数量呢?

我做不到...

  select count(*) from pitchers where secondary_pitch is null

最佳答案

我认为你可以只使用 json_length():

where json_length(secondary_pitch) = 0

关于mysql - 检查 MySQL 中的 JSON 值是否为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51114731/

相关文章:

sql - 带通配符的 Mysql select 语句

mysql从UTC转换为IST

SQL Server : Permissions on table

sql - 不要在 Laravel Eloquent ORM 中使用准备好的语句?

javascript - 从多个对象创建有效的 JSON

mysql多列查找

php - UTF-8贯穿始终

sql - 如何根据XSD从数据库导出数据到xml

php - Android Php Mysqli : Response cannot be parsed as JSON data

json - 使用 JSON 序列化 Gtk TreeStore/ListStore