2013-03-19 80 views
0

我有两个表,Fruits和Meals,Meals中的其中一列是varchar(100),其中包含水果。我正在改变这个,以便 这个列代替水果表中的水果的ID,我想通过比较这两个表并从水果列匹配的水果表中抓取ID 来设置它。将table 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. 
+0

@MahmoudGamal这是一个MS SQLServer的错误。 – 2013-03-19 20:28:48

回答

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

是的,这个剧本的作品,对于水果和餐食的混乱感到抱歉。 – 2013-03-20 12:40:09

相关问题