简体   繁体   English

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

[英]Copy table id from one column to another where column data matches between tables

I have two tables, Fruits and Meals, and one of the columns in Meals is a varchar(100) with the fruits in them. 我有两张桌子,水果和餐,而Meals中的一列是varchar(100),里面有水果。 I am changing this so that the column is instead the id of the fruit from the Fruits table, and I would like to set this by comparing the two tables and grabbing the id from the fruits table where the fruit columns match. 我正在更改这个,以便该列代替Fruits表中的水果的id,我想通过比较两个表并从水果表中的水果列匹配的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

I've tried the following script, but I get the following error. 我已经尝试了以下脚本,但是我收到以下错误。

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.

Depends on your RDBMS, but this should work for SQL Server: 取决于您的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

The Update Query can be simplified and a Join may be used instead of a subquery running the select statement. 可以简化Update Query,并且可以使用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

This runs on both SQL Server & Oracle. 这可以在SQL Server和Oracle上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将列数据从一个表复制到另一个表 - Copy column data from one table to another 将数据从一个表插入到另一个表中,其中一列有效地匹配两者 - Insert data from one table to another where one column matches in both efficiently SQL - 将数据从一个表列复制到另一个表列 - SQL - copy data from one table column to another table column SQL复制数据从一列到另一个表指定列 - SQL copy data from one column to another table specified column 从sqlite的不同表中将数据从一列复制到另一列 - Copy data from one column to another from different tables in sqlite 将列数据行(全部)插入到另一列表中,其中每一行都与公共ID匹配 - Insert column data rows (all) in another column table where each row matches the common ID 如何将数据从一个表复制到另一个表,其中列数据类型不同? - How to copy data from one table to another where column data types are different? 无法将数据从一个表复制到ID = 1的另一个表 - Unable to copy data from one table to another where id=1 如何在SQL中显示多个表中的数据,但仅当一个列数据与另一列匹配时才显示? - How to display data in SQL from multiple tables, but only if one column data matches another column? 将数据从一列复制到另一列 - Copy data from one column to another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM