我有两个表,一个是水果表,另一个是Meals表,其中一列是包含水果的varchar(100)。我对此进行了更改,使该列成为来自水果表的水果的id,我想通过比较这两个表并从水果列匹配的水果表中获取id来进行设置。
Table: Fruits
id | fruit
1 apple
2 banana
3 orange
Table: Meals
id | Meal | Fruit
1 xxxx apple
2 xxxx apple
3 xxxx orange
4 xxxx banana
5 xxxx orange
6 xxxx orange
7 xxxx apple我已经尝试了下面的脚本,但是我得到了以下错误。
Update product_attribute set control_caption =
(
Select DISTINCT T1.control_caption_id from control_caption T1
INNER Join product_attribute T2
On T1.control_caption = T2.control_caption
Where T1.control_caption = T2.control_caption
)
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.发布于 2013-03-20 04:29:03
这取决于您的RDBMS,但这应该适用于SQL Server:
Update pa
set pa.control_caption = cc.control_caption_id
From product_attribute pa
Join control_caption cc On
cc.control_caption = pa.control_caption发布于 2018-08-13 12:59:35
可以简化Update查询,并且可以使用联接来代替运行select语句的子查询。
Update P
Set P.Control_Caption = C.Control_Caption_ID
From Product_Attribute P
join Control_Caption C on C.Control_Caption= P.Control_Caption它可以在SQL Server和Oracle上运行。
https://stackoverflow.com/questions/15509694
复制相似问题