选择到具有不同列名的表中 [英] select into a table with different column names
问题描述
在 SQL 中,Select into ...
将行复制到不同的(备份)表中.如果备份表具有不同的结构(或不同的列名),这可能吗?如果没有,实现这一目标的最佳方法是什么?
In SQL, Select into ...
copies rows into a different (backup) table. Is this possible if the backup table has different structure (or different column names)? If not, what is the best way to achieve this?
这是我想要做的:TableA
有列 a1,a2,a3
.我想将此表中的一些行复制到另一个表 TableB
中,该表具有 b1,b2,b3,b4
列.a1
的内容进入b1
,a2
到b2
等
Here is what I want to do: TableA
has columns a1,a2,a3
. I want to copy some rows from this table to another table TableB
which has column b1,b2,b3,b4
. Content of a1
to go into b1
, a2
to b2
etc.
推荐答案
列名 根本不重要,只要数据类型匹配(或者可以是投在作业中).
The column names do not matter at all, as long as data types match (or can be cast in the assignment).
如果列的数据类型不匹配,请尝试相应地转换值.试试小虚拟桌子.请务必明确列出目标列以避免混淆.像这样:
If the data types of the columns don't match, try casting the values accordingly. Just try with small dummy tables. Be sure to list the target columns explicitly to avoid confusion. Like this:
INSERT INTO TableB (b1, b2, b3)
SELECT a1, a2, a3
FROM TableA
WHERE <some condition>;
SQLite 手册中的更多详细信息此处.
More details in the SQLite manual here.
这篇关于选择到具有不同列名的表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!