【问题标题】:How to execute this query in postgresql database i was in trouble to execute that如何在 postgresql 数据库中执行此查询我无法执行该查询
【发布时间】:2021-10-15 17:33:42
【问题描述】:

如何解决这个错误,我在运行该查询时遇到了麻烦,请任何人帮助执行该查询的所有属性。

PG_QUERY:-

UPDATE mapschema_127_17.layertable_2156_17 AS a 
SET "area(sqkm)" = newvalues.area(sqkm),
    "county" = newvalues.county,
    "countyfp" = newvalues.countyfp,
    "geoid" = newvalues.geoid,
    "gid" = newvalues.gid,
    "land(sqm)" = newvalues.land(sqm),
    "state" = newvalues.state,
    "statefp" = newvalues.statefp,
    "stusps" = newvalues.stusps,
    "water(sqm)" = newvalues.water(sqm) 
FROM 
    (VALUES ('2813.807491', 'Rich County', '033', '49033', 1, '2664700959', 'Utah', '49', 'UT', '149106532'),
            ('3037.725199', 'Cache County', '005', '49005', 2, '3016627502', 'Utah', '49', 'UT', '21097697'),
            ('8418.300607', 'Duchesne County', '013', '49013', 3, '8379502802', 'Utah', '49', 'UT', '38797805'),
            ('9543.91397', 'Grand County', '019', '49019', 28, '9512361692', 'Utah', '49', 'UT', '31552278'),
            ('18870.630612', 'Tooele County', '045', '49045', 29, '17979556898', 'Utah', '49', 'UT', '891073714')) AS newvalues ("area(sqkm)", "county", "countyfp", "geoid", "gid", "land(sqm)", "state", "statefp", "stusps", "water(sqm)") 
WHERE a.gid = newvalues.gid

错误:

错误:列“sqkm”不存在第 1 行:...able_2156_17 作为 SET "面积(平方公里)"=newvalues.area(平方公里),"cou... ^

其中area(sqkm) 是双精度数据类型的列名。

【问题讨论】:

  • 表mapschema_127_17.layertable_2156_17中是否存在列sqkm?
  • 列名为area(sqkm),存在于表mapschema_127_17.layertable_2156_17@JimJones
  • 这是一个非常“非正统”的列名:) 你能添加CREATE TABLE 声明吗?
  • @JimJones 我使用shp2pgsql添加此表
  • 然后不要将所有内容都变成字符串。我想'2813.807491', 'Rich County', '033', '49033', 1, '2664700959', 'Utah', '49', 'UT', '149106532' 应该是2813.807491, 'Rich County', '033', 49033, 1, 2664700959, 'Utah', 49, 'UT', 149106532?还是所有这些列都是字符串?

标签: sql postgresql postgis


【解决方案1】:

允许在对象名称中使用特殊字符 - 通过用引号将其包裹起来 " - 但通常会导致混淆。这个列名很奇怪的例子可能会让事情更清楚:

演示:db<>fiddle

CREATE TABLE t ("(id)" int, "#(txt)" text);
INSERT INTO t VALUES (1,'foo');

UPDATE t SET "#(txt)" = newvalues."#(txt)"
FROM (VALUES (1,'bar')) newvalues ("(id)","#(txt)")
WHERE t."(id)" = newvalues."(id)";

SELECT * FROM t;

 (id) | #(txt) 
------+--------
    1 | bar

这应该可以解决您的查询:

UPDATE mapschema_127_17.layertable_2156_17 AS a 
SET "area(sqkm)"=newvalues."area(sqkm)",
    "county"=newvalues."county",
    "countyfp"=newvalues."countyfp",
    "geoid"=newvalues."geoid",
    "gid"=newvalues."gid",
    "land(sqm)"=newvalues."land(sqm)",
    "state"=newvalues."state",
    "statefp"=newvalues."statefp",
    "stusps"=newvalues."stusps",
    "water(sqm)"=newvalues."water(sqm)" 
FROM 
  (VALUES 
    (2813.807491,'Rich County','033','49033',1,'2664700959','Utah','49','UT','149106532'),
    (3037.725199,'Cache County','005','49005',2,'3016627502','Utah','49','UT','21097697'),
    (8418.300607,'Duchesne County','013','49013',3,'8379502802','Utah','49','UT','38797805'),
    (9543.91397,'Grand County','019','49019',28,'9512361692','Utah','49','UT','31552278'),
    (18870.630612,'Tooele County','045','49045',29,'17979556898','Utah','49','UT','891073714')) AS newvalues ("area(sqkm)","county","countyfp","geoid","gid","land(sqm)","state","statefp","stusps","water(sqm)") 
WHERE a."gid" = newvalues."gid";

【讨论】:

  • 我试试这个但我得到了这个错误-ERROR: column "area(sqkm)" is of type double precision but expression is of type text LINE 5: SET "area(sqkm)"=newvalues."area(sqkm)",
  • @Ayush 这是一个完全不同的问题。您在double precision 列中插入text .. 这是不允许的。可以添加create table 语句吗?
  • @Ayush 我刚刚编辑了查询,修改了"area(sqkm)" 。
  • 我用column data type text 试过这个,它可以工作,但它不适用于我的数据,你能建议我可以用double precision 或其他一些数据类型也可以用这个查询。
  • @Ayush 你试过我的最后一次编辑了吗?我删除了 "area(sqkm)" 值周围的 ' 单引号
猜你喜欢
  • 1970-01-01
  • 2014-11-29
  • 1970-01-01
  • 1970-01-01
  • 2021-06-14
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多