简体   繁体   English

基于索引值将列从一个表复制到另一个表的 SQL 查询

[英]SQL query to copy column from one table to another table based off index value

I am trying to formulate a query that copies the contents of one column in a table to another table and match it up with the index values.我正在尝试制定一个查询,将表中一列的内容复制到另一个表并将其与索引值匹配。

Both tables are going to have the same index values.两个表都将具有相同的索引值。

Example:例子:

Table1表格1

+-----------------------------------+------------+
|               index               |    val     |
+-----------------------------------+------------+
|                 11                |     AA     |
|                 23                |     AA     |
|                 34                |     BB     |
+-----------------------------------+------------+

Table2表2

+-----------------------------------+
|               index               |
+-----------------------------------+
|                 34                |
|                 11                |
|                 23                |
+-----------------------------------+

After query is run, make table 2 reflect:查询运行后,使表 2 反映:

+-----------------------------------+------------+
|               index               |    val     |
+-----------------------------------+------------+
|                 34                |     BB     |
|                 11                |     AA     |
|                 23                |     AA     |
+-----------------------------------+------------+

I would appreciate any help offered.我将不胜感激提供的任何帮助。 Thanks in advance.提前致谢。

I don't quite understand why you would like to do this, but it could look something like:我不太明白你为什么要这样做,但它可能看起来像:

Alter table BTABLE add val varchar(10);
update BTABLE  bta set val = (select ata.val from ATABLE ata where ata.index = bta.index);

the only thing is, this way you would have 2 completely identical tables...唯一的问题是,这样你就会有 2 个完全相同的表......

The easiest way to do what you describe, is to drop Table2 and recreate it as a copy of Table1 :执行您所描述的操作的最简单方法是删除Table2并将其重新创建为Table1的副本:

DROP TABLE Table2;

CREATE TABLE Table2 AS
SELECT * FROM Table1;

See the demo .请参阅演示
Results (for Table2 ):结果(对于表Table2 ):

> index | val
> ----: | :--
>    11 | AA 
>    23 | AA 
>    34 | BB 

Like Irina Avram told you, Why do you want to copy a column that already exists in your database.就像 Irina Avram 告诉您的那样,您为什么要复制数据库中已存在的列。 You can use JOIN and a SELECT statement to get what you want, without adding more datas in your database.您可以使用 JOIN 和 SELECT 语句来获得所需的内容,而无需在数据库中添加更多数据。

SELECT Table2.index, Table1.val FROM Table2 JOIN Table1 ON Table2.index = Table1.index

Learn more about JOIN了解更多关于加入

暂无
暂无

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

相关问题 SQL查询:如何根据关闭ID将MIN值从一个表发送到另一个表 - SQL Query: How to send MIN value from one table to another based off ID Oracle - SQL 查询根据列值将数据从一个表分装到另一个表? - Oracle - SQL query to bin the data from one table to another based on a column value? SQL - 将数据从一个表列复制到另一个表列 - SQL - copy data from one table column to another table column SQL-基于一个表与另一个表的联接(第一个表的行值到第二个表的列值) - SQL - Joining one table with another based on ( Row value from first table to Column value of second table) 在BigQuery SQL中,将一列从一个表复制到另一表。 - Copy a column from one table into another table in BigQuery SQL. 如何将列从一个表复制到 SQL 服务器中的另一个表 - How to copy column from one table into another table in SQL Server SQL-更新表基于另一表更改一个列的值 - SQL - Update table changing one column value based on another table 如何通过匹配列名将列数据从一个表复制到另一个表? 在 SQL 或 Power Query 中 - How to Copy column data from one table to another by matching Column names? in SQL or Power Query SQL复制数据从一列到另一个表指定列 - SQL copy data from one column to another table specified column 在SQL中,如何基于另一个字段的值将值从一个表复制到另一个表? - In SQL How do I copy values from one table to another based on another field's value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM