简体   繁体   English

Postgresql:将两个值添加到以逗号分隔的列?

[英]Postgresql: add two values to a column separated by a comma?

I'm trying to create a column named geo that has a column that combines two randomly generated points.我正在尝试创建一个名为 geo 的列,其中有一列组合了两个随机生成的点。 My table looks like this.我的桌子看起来像这样。

create table data (
    id SERIAL primary key,
    first VARCHAR(50),
    last VARCHAR(50),
    g VARCHAR(50),
    geo VARCHAR(100)
);

Here is my try at generating those random values but get an error because the INSERT has more expression than target columns这是我尝试生成这些随机值但出现错误,因为INSERT has more expression than target columns

INSERT INTO data (first,last,g,geo)
SELECT substr(md5(random()::text), 1, 7),
       substr(md5(random()::text), 1, 10),
       substr(md5(random()::text), 1, 1),
       (random() * (47.606209 - 25.427152)) + 25.427152 , (random() * (-124.389641 - -69.082237)) + -69.082237
FROM generate_series(1, 20);

Does anyone know how to do this?有谁知道如何做到这一点? Also, I know geo locations shouldn't be strings but in my case it works perfectly.另外,我知道地理位置不应该是字符串,但在我的例子中它工作得很好。 Thanks谢谢

The comma between the values indicates they are two different columns.值之间的逗号表示它们是两个不同的列。

INSERT INTO data (first,last,g,geo)
SELECT
  -- first
  substr(md5(random()::text), 1, 7),
  -- last
  substr(md5(random()::text), 1, 10),
  -- g
  substr(md5(random()::text), 1, 1),
  -- geo
  (random() * (47.606209 - 25.427152)) + 25.427152,
  -- ?fifth column?
  (random() * (-124.389641 - -69.082237)) + -69.082237
FROM generate_series(1, 20);

If you want to concatenate strings use ||如果要连接字符串,请使用|| . .

INSERT INTO data (first,last,g,geo)
SELECT
  -- first
  substr(md5(random()::text), 1, 7),
  -- last
  substr(md5(random()::text), 1, 10),
  -- g
  substr(md5(random()::text), 1, 1),
  -- geo
  (random() * (47.606209 - 25.427152)) + 25.427152 ||
    ',' ||
    (random() * (-124.389641 - -69.082237)) + -69.082237
FROM generate_series(1, 20);

However, storing these two numeric values as a string will make it slow and difficult to use.但是,将这两个数值存储为字符串会使其变慢且难以使用。 Instead, use two numeric columns to store the two numbers.相反,使用两个numeric列来存储这两个数字。

create table data (
    id bigserial primary key,
    -- Don't put artificial limits on columns, it doesn't save space.
    -- They'll only use as much space as necessary.
    -- Use `text` unless there's a good reason for a limit.
    -- In Postgres, `text` is just an unlimited `varchar`.
    first text,
    last text,
    g text,
    geox numeric,
    geoy numeric
);

INSERT INTO data (first,last,g,geox,geoy)
SELECT
  -- first
  substr(md5(random()::text), 1, 7),
  -- last
  substr(md5(random()::text), 1, 10),
  -- g
  substr(md5(random()::text), 1, 1),
  -- geox
  (random() * (47.606209 - 25.427152)) + 25.427152,
  -- geoy
  (random() * (-124.389641 - -69.082237)) + -69.082237
FROM generate_series(1, 20);

Now they can be compared as numbers and they can be formatted as you need.现在它们可以作为数字进行比较,并且可以根据需要进行格式化。

select geox || ',' || geoy from data;

Better yet, use the built inpoint type .更好的是,使用内置的point类型

create table data (
    id bigserial primary key,
    first text,
    last text,
    g text,
    geo point
);

These can also be generated as x,y strings and cast to a point .这些也可以生成为x,y字符串并转换为一个point

select (random() || ',' || random())::point;

And Postgres has plenty ofbuilt in 2-D geometric functions . Postgres 有大量内置的二维几何函数

If you want to do real GIS, use PostGIS .如果您想做真正的 GIS,请使用PostGIS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM