【问题标题】:MySQL: If a selected field in stored procedure has NULL value I want to return N/A as valueMySQL:如果存储过程中的选定字段具有 NULL 值,我想返回 N/A 作为值
【发布时间】:2018-02-15 22:42:47
【问题描述】:

我正在使用phpmyadmin,并且我有以下存储过程:

SELECT im.onset_date,
im.end_date,
im.coding_id,
im.patient_id,
im.resolution_circumstances, 
FROM illness_mapping as im
WHERE im.patient_id = p_id and im.end_date <= CURDATE()

p_id 值是用户插入的。

在某些记录中,im.resolution_circumstances 为空。当我调用存储过程时,我想查找它是否为空并返回值N/A。我只想返回值,而不是将其存储到数据库中的特定字段中。

你有什么想法吗?

【问题讨论】:

  • 你要使用COALESCE
  • @SamiKuhmonen 你有如何使用它并将其合并到我的代码中的示例吗?

标签: mysql stored-procedures phpmyadmin


【解决方案1】:

您可以使用case 来检查您的属性是否为空

 SELECT im.onset_date,
    im.end_date,
    im.coding_id,
    im.patient_id,
(case 
    when im.resolution_circumstances is null 
       then "N/A"
    when im.resolution_circumstances is not null 
       then im.resolution_circumstances
  end) as resolution_circumstances
    FROM illness_mapping as im
    WHERE im.patient_id = p_id and im.end_date <= CURDATE()

【讨论】:

    猜你喜欢
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多