选择、修改和插入同一个表 [英] Select, Modify and insert into the same table
问题描述
选择一行、修改几列并将其插入同一个表的最简单方法是什么?我正在尝试根据另一行插入一个新行.
What's the easiest way to select a row, modify couple of columns and insert it to the same table? I'm trying to insert a new row based on another.
推荐答案
INSERT INTO table2 (column1, column2, column3)
SELECT column1, 'no', column3 FROM table2 WHERE column2 = 'yes'
希望您能更清楚地了解如何执行此操作.如您所见,我从 table2 中抓取了两列,对于另一列,我使用了文本值而不是 column2 的值.
Hopefully this is a bit clearer as to how you do this. As you can see, I've grabbed two columns from table2 and for the other column I used a text value for instead of the value for column2.
您可以使用的其他模式:
Other patterns you can use:
组合一列和一些其他文本(假设该列已经是字符串数据类型.
Combine a column and some other text (Assumes the column is already a string data type.
INSERT INTO table2 (column1, column2)
SELECT column1 + 'no', column2 FROM table2 WHERE column2 = 'yes'
组合一列和一些文本,一个是列是字符串的例子,另一个不是.
Combine a column and some text, One example where the column is a string and one where it is not.
INSERT INTO table2 (column1, column2)
SELECT column1 + 'no', 'A' + cast(column2 as Varchar (10)) FROM table2 WHERE column2 = 'yes'
这篇关于选择、修改和插入同一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!