【问题标题】:insert into a table with a definite "label"插入带有明确“标签”的表中
【发布时间】:2013-01-15 11:01:30
【问题描述】:

我已经创建了一个包含数据的表,我想添加更多行。 如果我执行以下操作,这些行确实会完美显示:

select  elem_1, elem_2, 'myTag' as source, count(*) as tally 
from origin_table group by elem_1, elem_2;

我所做的基本上是从 origin_table 中获取数据,并通过 elem_1 和 elem_2 进行选择,我将标签“myTag”附加到这些对中的每一对。由于我可能有重复项,因此我也将 count(*) 视为计数,以知道我在 origin_table 中有多个相等的条目。这是一个可以避免的细节。

现在,如果我继续做广告,请执行以下操作:

insert in to destination_table 
select  elem_1, elem_2, 'myTag' as source, count(*) as tally 
from origin_table group by elem_1, elem_2;

postgres 吠声:

ERROR:  invalid input syntax for integer: "myTag"
LINE 1: insert into destination_table select elem_1, elem_2, 'myTag' as sou...

我明白,但我不喜欢,因为我不知道如何克服它。

那么,我该怎么做呢?

谢谢!

【问题讨论】:

  • 你写的像in to,真的吗? (应该是INTO)。
  • 强烈建议您在插入语句 (insert into destination_table (col1, col2, col3) ... ) 中列出列以避免出现问题。如果这是您的问题的原因,我不会感到惊讶
  • @Anton:嗯。不,我和其他人一样写 INTO,但有时我忘记正确输入:)
  • @horse:马上试试。这是我应该遵循的好习惯。

标签: postgresql select insert


【解决方案1】:

按照 a_horse_with_no_name 的建议,我将插入转换为:

insert into destination_table (elem_1,elem_2,source,tally) select elem_1, elem_2, 'myTag' as source, count(*) as tally from origin_table group by elem_1, elem_2;

而且效果很好。 我真的应该养成告诉 postgres 我要插入什么以及在哪里插入的习惯。

谢谢!

【讨论】:

  • 有点吹毛求疵:指定插入列不称为“casting”。强制转换是指将值更改为与原始数据类型不同的数据类型。例如。一个数字作为字符串,一个字符串作为日期。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 1970-01-01
  • 1970-01-01
  • 2014-11-02
  • 2011-07-30
  • 2016-10-18
相关资源
最近更新 更多