2015-02-09 76 views
0

我想更新一個表,必須測試每一行,並且文本字段必須根據另一個表的主鍵更新爲numéric值。 3個可能的文本值可以與3個數值中的一個相關聯。我發現這個請求,它在每一行返回相同的第一個數值,'2'!從Sqlite中的案例更新

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 

所以,我可以通過彎曲試驗和更新的每一行,在第一個我將使用選擇指令返回一行,其次我會更新它...這可能是一個有效的解決方案,但它會做很多事情,沒有多少工作。

+0

您提到另一個表,但SQL只使用一個表。 – 2015-02-09 12:06:12

回答

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) 
+0

好,簡單而高效。 – 2015-02-11 09:10:23