首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将表id从一列复制到另一列,其中列数据在表之间匹配

将表id从一列复制到另一列,其中列数据在表之间匹配
EN

Stack Overflow用户
提问于 2013-03-20 04:24:40
回答 2查看 651关注 0票数 0

我有两个表,一个是水果表,另一个是Meals表,其中一列是包含水果的varchar(100)。我对此进行了更改,使该列成为来自水果表的水果的id,我想通过比较这两个表并从水果列匹配的水果表中获取id来进行设置。

代码语言:javascript
复制
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

我已经尝试了下面的脚本,但是我得到了以下错误。

代码语言:javascript
复制
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.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-20 04:29:03

这取决于您的RDBMS,但这应该适用于SQL Server:

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2018-08-13 12:59:35

可以简化Update查询,并且可以使用联接来代替运行select语句的子查询。

代码语言:javascript
复制
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上运行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15509694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档