Sql 将表id从一列复制到表之间列数据匹配的另一列
我有两张桌子,水果和餐食,餐食中的一列是一个装有水果的varchar100。我正在改变这一点,以便 列是水果表中水果的id,我想通过比较两个表并获取id来设置它 从水果列匹配的水果表中Sql 将表id从一列复制到表之间列数据匹配的另一列,sql,sql-server,Sql,Sql Server,我有两张桌子,水果和餐食,餐食中的一列是一个装有水果的varchar100。我正在改变这一点,以便 列是水果表中水果的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 xx
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.
取决于您的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
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上运行。@MahmoudGamal这是一个MS SQL Server错误。是的,此脚本工作正常,很抱歉对水果和膳食造成混淆。