0

我有两张桌子,Fruits 和 Meals,Meals 中的一列是 varchar(100),里面有水果。我正在更改它,以便该列是 Fruits 表中水果的 id,我想通过比较两个表并从水果列匹配的 fruits 表中获取 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.
4

2 回答 2

2

取决于您的 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
于 2013-03-19T20:29:02.580 回答
0

可以简化更新查询,并且可以使用 Join 来代替运行 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 上运行。

于 2018-08-13T04:59:34.683 回答