川陀总督 2022-03-04 17:26 采纳率: 100%
浏览 809
已结题

mysql的json_extract函数碰到部分数据为空报错

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图

select id,patient_name,hospital_name,
TIMESTAMPDIFF(YEAR, data_content->>'$.userInfo.patient_birth', CURDATE()) as patient_age,
data_content->>'$.userInfo.patient_height' as patient_height,
data_content->>'$.userInfo.patient_weight' as patient_weight,
data_content->>'$.userInfo.patient_gender' as patient_gender,
data_content->>'$.report_type' as report_type,
data_content->>'$.report.stepPhase' as stepPhase,
json_extract(data_content->>'$.report.stepPhase','$.相位图."站立相中期"') as 站立相中期
from mrc_report
where hospital_name = '积水潭医院' and report_type != "balance"

运行结果及报错内容

Invalid JSON text in argument 1 to function json_extract: "The document is empty." at position 0.

我的解答思路和尝试过的方法

我检查了数据集,report.stepPhase确实有几个空的。我尝试了把json_valid(data_content->>'$.report.stepPhase')加在了最后的where条件中,能够出结果,但是结果不包括空值的几条数据
from mrc_report
where hospital_name = '积水潭医院' and report_type != "balance"
and json_valid(data_content->>'$.report.stepPhase')

我想要达到的结果

img

这个结果是不包括空数据的,如果某条数据为空,保留report_type前的信息,最后一列显示为null

  • 写回答

1条回答 默认 最新

  • DarkAthena ORACLE应用及数据库设计方案咨询师 2022-03-05 21:54
    关注

    条件加下面不行,那就加上面呗

    select id,patient_name,hospital_name,
    case when json_valid(data_content->>'$.report.stepPhase') then 
    data_content->>'$.report.stepPhase' end as stepPhase
    from mrc_report
    where hospital_name = '积水潭医院' and report_type != "balance"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月29日
  • 已采纳回答 3月21日
  • 修改了问题 3月4日
  • 创建了问题 3月4日