我有一个关于SQL语法的问题,以及在单个查询中是否可以执行特定操作.请考虑以下示例:
假设我们有一个Recipe表.每个食谱都有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')
Run Code Online (Sandbox Code Playgroud)
我在网上发现了多篇关于根据列当前值将列设置为某个值的文章,但不仅仅是设置包含值的特定列.任何帮助是极大的赞赏.
我会用"你不想听到的"答案回答你的问题.您错误地设计了数据库.
你应该有这样的结构:
Recipe
RecipeID, other recipe information
Ingredient
IngredientID, RecipeID, Ingredient, Measurement...
Run Code Online (Sandbox Code Playgroud)
理想情况下,实际上,您可能拥有Ingredient查找表并且具有RecipeIngredient测量信息的连接表IngredientID,但是上述内容仍然会比您拥有的要好得多.
然后你的更新将是:
update Ingredient
set Ingredient='artificial sweetener'
where Ingredient='sugar'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1259 次 |
| 最近记录: |