1.使用场景:在MySQL中,JSON作为一种数据类型存储在表的列中。需计算键值对的数量。
2.方法:SELECT COUNT(chief>'$.number') FROM t_projectapplication where id = #{id};
(t_projectapplication:表;chief:包含JSON类型数据的列名;number:JSON数据中的某个键名);
3.进阶使用场景:计算包含两个JSON类型数据字段之和;
4.方法:SELECT COUNT(cnt) FROM ( SELECT COUNT(chief>'$.number') AS cnt FROM t_projectapplication where id = #{id} UNION ALL SELECT COUNT(department_leader>'$.number') AS cnt FROM t_projectapplication where where id = #{id} ) AS countdata