从 Sqlite 中的案例更新

Update from a Case in Sqlite

我想更新一个 table,必须测试每一行,并且必须根据另一个 table 的主键将文本字段更新为数字值。 3 个可能的文本值可以与 3 个数值之一相关联。我发现了这个请求,其中 return 在每一行都有相同的第一个数值,'2' !

UPDATE HIV_adults_or_women_or_children_living_with_HIV SET indicateur_reduit=(
CASE 
WHEN ((select indicateur_in FROM HIV_adults_or_women_or_children_living_with_HIV) = "Number of adults aged 15 and over living with HIV") 
THEN (1)
WHEN ((SELECT indicateur_in FROM HIV_adults_or_women_or_children_living_with_HIV) = "Number of women aged 15 and over living with HIV" )
THEN (2)
WHEN ((select indicateur_in FROM HIV_adults_or_women_or_children_living_with_HIV) = "Number of children under 15 living with HIV")  
THEN (3)
END )

此外,我尝试了下一个无效,因为它给出了 "not such column idResult" :

UPDATE HIV_adults_or_women_or_children_living_with_HIV SET indicateur_reduit=(
SELECT (SELECT ID FROM HIV_adults_or_women_or_children_living_with_HIV) as idResult,
CASE 
WHEN ((SELECT indicateur_in FROM HIV_adults_or_women_or_children_living_with_HIV WHERE id) = "Number of women aged 15 and over living with HIV" )
THEN (1)
WHEN ((select indicateur_in FROM HIV_adults_or_women_or_children_living_with_HIV WHERE id) = "Number of children under 15 living with HIV")  
THEN (2)
WHEN ((select indicateur_in FROM HIV_adults_or_women_or_children_living_with_HIV WHERE id) = "Number of adults aged 15 and over living with HIV") 
THEN (3)
END )
WHERE id=idResult

所以我可以通过 Flex 测试和更新每一行,首先我将使用 select 指令到 return 一行,然后我将更新它......这可能是一个有效的解决方案,但它会无事生非,有很多工作要做。

您不需要子查询。据推测,所有信息都来自一行:

UPDATE HIV_adults_or_women_or_children_living_with_HIV
    SET indicateur_reduit = (CASE WHEN indicateur_in = 'Number of adults aged 15 and over living with HIV'
                                  THEN 1
                                  WHEN indicateur_in = 'Number of women aged 15 and over living with HIV'
                                  THEN 2
                                  WHEN indicateur_in = 'Number of children under 15 living with HIV'
                                  THEN 3
                              END )