【问题标题】:SQL database calculation for null variable using other components使用其他组件对空变量进行 SQL 数据库计算
【发布时间】:2020-11-04 18:51:53
【问题描述】:

我希望通过在医疗保健计算器上使用预定义权重来计算空变量,并且我希望输出是变量权重与总可用权重的总和。正如您在下面看到的,希望是“sum/8”。

CREATE TABLE `trial`.`trial` ( `Name` TEXT NULL , `Age` INT NULL , `BP systolic` INT NULL , `BP diastolic` INT NULL ,`Clinical features of the TIA` TEXT NULL , `Duration of symptoms` INT NULL , `History of diabetes` TEXT NULL , `ABCD² Score for TIA` FLOAT NULL ) ENGINE = InnoDB;

INSERT INTO `trial` (`Name`, `Age`, `BP systolic`, `BP diastolic`, `Clinical features of the TIA`, `Duration of symptoms`, `History of diabetes`, `ABCD² Score for TIA`) VALUES ('Person A', '71', '137', '85', 'Speech disturbance without weakness', '17', 'Yes', NULL);

INSERT INTO `trial` (`Name`, `Age`, `BP systolic`, `BP diastolic`, `Clinical features of the TIA`, `Duration of symptoms`, `History of diabetes`, `ABCD² Score for TIA`) VALUES ('Person B', '92', '125', '78', 'Other symptoms', '43', 'Yes', NULL);
INSERT INTO `trial` (`Name`, `Age`, `BP systolic`, `BP diastolic`, `Clinical features of the TIA`, `Duration of symptoms`, `History of diabetes`, `ABCD² Score for TIA`) VALUES ('Person C', '27', '130', '90', 'Other symptoms', '34', 'No', NULL);

update trial 
set ABCD² Score for TIA = case when ( 
case when Age = >=60 then 1 else 0 end 
+ case when BP systolic = >= 140 then 1 else 0 end 
+ case when BP diastolic = >=90 then 1 else 0 end 
+ case when Clinical features of the TIA = 'Unilateral weakness' then 2 end 
+ case when Clinical features of the TIA = 'Speech disturbance without weakness' then 1 end
+ case when Clinical features of the TIA = 'Other symptoms' then 0 end 
+ case when Duration of symptoms = <10   then 0 end 
+ case when Duration of symptoms = 10-59 then 1 end 
+ case when Duration of symptoms = >= 60 then 2 end
+ case when History of diabetes = 'Yes' then 1 else 0 end
) sum/8 
where ABCD² Score for TIA is null

【问题讨论】:

  • 你不能写= &gt;=60。它应该只是&gt;= 60
  • 为什么有嵌套的 case 表达式?
  • 在列名中有空格会使事情变得更加复杂。在这里,您需要在查询中引用所有这些标识符。

标签: mysql sql phpmyadmin


【解决方案1】:

您必须修复您的 CASE 表达式并使用 MySql 的功能将布尔表达式计算为 1 或 0:

update trial 
set `ABCD² Score for TIA` = ( 
  (Age >= 60) + (`BP systolic` >= 140) + (`BP diastolic` >= 90) +
  case `Clinical features of the TIA`
    when 'Unilateral weakness' then 2 
    when 'Speech disturbance without weakness' then 1 
    when 'Other symptoms' then 0 
  end +  
  case
    when `Duration of symptoms` >= 60 then 2
    when `Duration of symptoms` >= 10 then 1
    when `Duration of symptoms` < 10 then 0
  end + 
  (`History of diabetes` = 'Yes')
) / 8 
where `ABCD² Score for TIA` is null

请参阅demo
结果:

> Name     | Age | BP systolic | BP diastolic | Clinical features of the TIA        | Duration of symptoms | History of diabetes | ABCD² Score for TIA
> :------- | --: | ----------: | -----------: | :---------------------------------- | -------------------: | :------------------ | -------------------:
> Person A |  71 |         137 |           85 | Speech disturbance without weakness |                   17 | Yes                 |                  0.5
> Person B |  92 |         125 |           78 | Other symptoms                      |                   43 | Yes                 |                0.375
> Person C |  27 |         130 |           90 | Other symptoms                      |                   34 | No                  |                 0.25

【讨论】:

    【解决方案2】:

    当值为 1 或 0 时,你不需要 CASE 表达式。条件为 true 时的值为 1,否则为 0。所以直接使用条件就好了。

    而且您不需要将这些案例嵌套在另一个案例中。

    您需要在所有包含空格的列名周围加上反引号。

    UPDATE trial
    SET `ABCD² Score for TIA` = (
        Age >= 60
        + `BP systolic` >= 140
        + `BP diastolic >= 90`
        + (case `Clinical features of the TIA`
                WHEN 'Unilateral weakness' then 2
                WHEN 'Speech disturbance without weakness' THEN 1
                ELSE 0
           END)
        + (case when `Duration of symptoms` BETWEEN 10 AND 59 THEN 1
                when `Duration of symptoms` >= 60 then 2
                ELSE 0 
           END)
        + `History of diabetes` = 'Yes')
    WHERE `ABCD² Score for TIA` IS NULL
    

    【讨论】:

      猜你喜欢
      • 2019-03-22
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多