从Sqlite中的Case更新

时间:2015-02-09 10:51:46

标签: sql sqlite case

我想更新表,必须测试每一行,并且必须根据另一个表的主键将文本字段更新为numéric值。 3个可能的文本值可以与3个数值中的一个相关联。我找到了这个请求,它在每一行返回相同的第一个数字值'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 )

此外,我尝试了下一个无效的,因为它给出了“不是这样的列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指令返回一行,然后我会更新它......这可能是一个有效的解决方案,但它会很有趣什么都没有,很多工作。

1 个答案:

答案 0 :(得分:0)

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

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 )