2014-08-29 95 views
0

我有一个表称为Records具有数据作为以下各项如何在同一个表中的Postgres插入相同的数据(或复制)为不同的记录

Section_one | section_two | values  | cDate 

name_Fit | hellp0  | present  | 2014-08-23 
name_Fit | onew  | parcel  | 2014-08-21 
name_Fit | twow  | new thing | 2014-07-04 

现在我想插入一个名为新数据name_Fit_one与第section_one列相对应,其应该具有与其相对应的类似数据(列section_twovalues)。

我试图写的SQL来做到这一点,但我得到的postgres错误。任何人都可以请纠正我我做错了什么?

Insert into records(section_one,section_two,values) Values ('name_Fit_one', select section_two,values from records where section_one='name_Fit'); 

预期结果

Section_one | section_two | values  | cDate 

name_Fit | hellp0  | present  | 2014-08-23 
name_Fit | onew  | parcel  | 2014-08-21 
name_Fit | twow  | new thing | 2014-07-04 
name_Fit_one| hellp0  | present  | 2014-08-29 
name_Fit_one| onew  | parcel  | 2014-08-29 
name_Fit_one| twow  | new thing | 2014-08-29 

需要编写一个将记录的内容复制到另一个记录单查询?

下面是它 -

http://sqlfiddle.com/#!2/9de58/1

+0

你的标签说postgresql,但你的sqlfiddle是mysql。你真的在用什么数据库?你的数据也显示一列“值”,但你的小提琴显示“价值”,这是正确的?也是cdate时间戳字段(如在你的小提琴)或日期字段(如上面的数据)? – 2014-08-29 21:59:54

回答

1

如果你使用PostgreSQL,你应该能够使用:

Insert into records 
    select 'name_Fit_one', section_two, values, '2014-08-29' 
    from records 
    where section_one = 'name_Fit'; 

小提琴:http://sqlfiddle.com/#!15/fbbed/1/0

注意,我改名一些你列的,因为它是不清楚你上面的例子或你的sqlfiddle包含你的实际数据和列名(它们不同)。

如果您希望它是运行插入的日期,那么可以用CURRENT_DATE替换文字'2014-08-29'。

1
Insert into records (section_one,section_two, "values") 
select 'name_Fit_one', section_two, "values" 
from records 
where section_one='name_Fit'; 

注意values是一个保留字,因此需要加引号的SQL小提琴。

相关问题