简体   繁体   English

从sqlite的不同表中将数据从一列复制到另一列

[英]Copy data from one column to another from different tables in sqlite

I want to copy data to column A in Table1 from column B in Table2. 我想将数据从Table2的B列复制到Table1的A列。 Rows for column A are empty and there are exists other columns in Table1 with already populated data. A列的行为空,并且Table1中存在其他已填充数据的列。 So I need to grab the whole column B from Table2 and insert all those values in column A in Table1. 因此,我需要从Table2中获取整个B列,并将所有这些值插入Table1中的A列中。 The two table are completely identical, except that column A has no values at all. 这两个表完全相同,除了A列根本没有值。

How do I do this in sqlite3? 我如何在sqlite3中做到这一点?

Use: 采用:

INSERT INTO TABLE1
SELECT B,
       NULL,
       NULL,
       NULL
  FROM TABLE2

Use NULL as the placeholder for however many columns you can't populate from TABLE2, assuming TABLE1 columns allow NULL values. 假设TABLE1列允许使用NULL值,则对于无法从TABLE2填充的许多列,请使用NULL作为占位符。

尝试以下操作:插入表1(A)从表2选择B

UPDATE TABLE1 SET A = (SELECT B FROM TABLE2 WHERE ...)

Come to think of it, if the tables are truly identical, why do you need two of them? 试想一下,如果这些表确实相同,那么为什么需要其中两个表呢? In any case you can also do this: 无论如何,您也可以执行以下操作:

BEGIN;
DELETE FROM TABLE1;
INSERT INTO TABLE1 (A, col1, col2, ...) SELECT (B, col2, col2, ...) FROM TABLE2;
COMMIT;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM