0

我有一个关于 SQL 语法以及在单个查询中是否可以执行特定操作的问题。考虑以下示例:

假设我们有一个食谱表。每个食谱都有 5 种成分列在成分 1、成分 2 等列下。假设突然确定糖对您不健康,并且每个食谱必须在其存在的每个成分列中将“糖”替换为“人造甜味剂”。我遇到的问题是没有成分表,因此每种成分都列在五列之一的下面,而不是引用成分 ID 之类的东西。

如果可能,我如何编写以下(伪)查询?

UPDATE Recipe 
    (SET ingredient1 = 'artificial sweetener' WHERE ingredient1 = 'sugar') OR
    (SET ingredient2 = 'artificial sweetener' WHERE ingredient2 = 'sugar') OR
    (SET ingredient3 = 'artificial sweetener' WHERE ingredient3 = 'sugar') OR
    (SET ingredient4 = 'artificial sweetener' WHERE ingredient4 = 'sugar') OR
    (SET ingredient5 = 'artificial sweetener' WHERE ingredient5 = 'sugar')

我在网上找到了多篇关于根据当前值将列设置为某个值的文章,但不仅仅是设置包含值的(特定)列。任何帮助是极大的赞赏。

4

5 回答 5

6

我会用“不是你想听到的”答案来回答你的问题。您错误地设计了数据库。

您应该具有以下结构:

Recipe
RecipeID, other recipe information

Ingredient
IngredientID, RecipeID, Ingredient, Measurement...

理想情况下,实际上,您可能会拥有Ingredient一个查找表,并拥有RecipeIngredient一个包含测量信息的连接表 和IngredientID,但上述内容仍然比您拥有的要好得多。

那么您的更新将是:

update Ingredient 
    set Ingredient='artificial sweetener' 
    where Ingredient='sugar'
于 2012-04-24T19:29:05.137 回答
4

A CASE expression will dictate whether each of the five columns need to be modified. Use a WHERE clause to filter out any rows that don't require modification:

UPDATE Recipe
SET ingredient1 = CASE WHEN ( ingredient1 = 'sugar' )  
    THEN 'artificial sweetener' ELSE ingredient1 END ,
SET ingredient2 = CASE WHEN ( ingredient2 = 'sugar' )  
    THEN 'artificial sweetener' ELSE ingredient2 END ,
SET ingredient3 = CASE WHEN ( ingredient3 = 'sugar' )  
    THEN 'artificial sweetener' ELSE ingredient3 END ,
SET ingredient4 = CASE WHEN ( ingredient4 = 'sugar' )  
    THEN 'artificial sweetener' ELSE ingredient4 END ,
SET ingredient5 = CASE WHEN ( ingredient5 = 'sugar' )  
    THEN 'artificial sweetener' ELSE ingredient5 END
WHERE 'sugar' IN 
  ( ingredient1, ingredient2, ingredient3, ingredient4, ingredient5 )
;
于 2012-04-24T19:33:11.033 回答
1

考虑重新设计您的数据库,您当前的设计不是很友好。无论如何,您正在寻找的答案是使用 case-when update 子句;

UPDATE tablename
SET 
INGREDIENT1 = CASE WHEN INGREDIENT1='SUGAR' THEN 'ARTIFICIAL SWEETENER' ELSE INGREDIENT1    END,
INGREDIENT2 = CASE WHEN INGREDIENT2='SUGAR' THEN 'ARTIFICIAL SWEETENER' ELSE INGREDIENT2 END
于 2012-04-24T19:35:10.077 回答
0

您可以执行以下操作:

UPDATE Recipe SET 
       ingredient1 = if(ingredient1 = 'sugar', 'artificial sweetener', ingredient1),
       ingredient2 = if(ingredient2 = 'sugar', 'artificial sweetener', ingredient2),
       ..etc..
于 2012-04-24T19:25:53.623 回答
0

以下是一些可移植的:

UPDATE Recipe
SET ingredient1 = CASE ingredient1 WHEN 'sugar' THEN 'artificial sweetener' ELSE ingredient1 END,
SET ingredient2 = CASE ingredient2 WHEN 'sugar' THEN 'artificial sweetener' ELSE ingredient2 END,
SET ingredient3 = CASE ingredient3 WHEN 'sugar' THEN 'artificial sweetener' ELSE ingredient3 END,
SET ingredient4 = CASE ingredient4 WHEN 'sugar' THEN 'artificial sweetener' ELSE ingredient4 END,
SET ingredient5 = CASE ingredient5 WHEN 'sugar' THEN 'artificial sweetener' ELSE ingredient5 END
WHERE ingredient1 = 'sugar'
    OR ingredient2 = 'sugar'
    OR ingredient3 = 'sugar'
    OR ingredient4 = 'sugar'
    OR ingredient5 = 'sugar'

请注意,只要配方中有糖,这在技术上会更新所有这些列,而不管当前值如何。这是使用单个UPDATE语句和此模式的结果。查询优化器通常考虑整个行和列,而不是单个值。

于 2012-04-24T19:42:00.350 回答