从select table2插入表1

k2fxgqgv  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(231)

我有以下两张table:

postgresp=> \d tempo
                          Table "postgres.tempo"
   Column   |          Type          | Collation | Nullable | Default 
------------+------------------------+-----------+----------+---------
 user_id    | integer                |           |          | 
 session_id | bigint                 |           |          | 
 lat        | double precision       |           |          | 
 lon        | double precision       |           |          | 
 flag       | integer                |           |          | 
 alt        | double precision       |           |          | 
 days       | double precision       |           |          | 
 gpsdate    | date                   |           |          | 
 gpstime    | time without time zone |           |          |

postgres=> \d trajectory
                        Table "postgres.trajectory"
   Column   |           Type           | Collation | Nullable | Default 
------------+--------------------------+-----------+----------+---------
 user_id    | integer                  |           |          | 
 session_id | bigint                   |           | not null | 
 timestamp  | timestamp with time zone |           | not null | 
 lat        | double precision         |           |          | 
 lon        | double precision         |           |          | 
 alt        | double precision         |           |          | 
Indexes:
    "trajectory_pkey" PRIMARY KEY, btree (session_id, "timestamp")

然后我想插入 tempo 进入 trajectory 签署人:
将1添加到 user_id 康卡安特 gpsdate 以及 gpstime 来自的字段 tempo 进入 timestamp 字段输入 trajectory .
查询:

INSERT INTO trajectory (user_id, session_id, timestamp, lat, lon, alt)
SELECT user_id + 1
    , session_id

    , CONCAT(gpsdate, ' ', gpstime)
    , lat
    , lon
    , alt
FROM tempo

错误:

ERROR:  column "timestamp" is of type timestamp with time zone but expression is of type text
LINE 4:  , CONCAT (gpsdate, ' ', gpstime)
           ^
HINT:  You will need to rewrite or cast the expression.

数据库中的数据 tempo 表格:

posgres=> SELECT * FROM tempo LIMIT 10;
 user_id |   session_id   |    lat    |    lon     | flag | alt |       days       |  gpsdate   | gpstime  
---------+----------------+-----------+------------+------+-----+------------------+------------+----------
       0 | 20090429005954 | 40.008048 | 116.316474 |    0 | 491 | 39932.0415972222 | 2009-04-29 | 00:59:54
       0 | 20090429005954 | 40.009643 | 116.317842 |    0 | 300 | 39932.0416550926 | 2009-04-29 | 00:59:59
       0 | 20090429005954 | 40.009794 | 116.318053 |    0 | 283 |  39932.041712963 | 2009-04-29 | 01:00:04
       0 | 20090429005954 | 40.009646 |  116.31813 |    0 | 276 | 39932.0417708333 | 2009-04-29 | 01:00:09
       0 | 20090429005954 | 40.009554 | 116.318121 |    0 | 273 | 39932.0418287037 | 2009-04-29 | 01:00:14
       0 | 20090429005954 | 40.009432 | 116.318115 |    0 | 269 | 39932.0418865741 | 2009-04-29 | 01:00:19
       0 | 20090429005954 |   40.0093 | 116.318127 |    0 | 266 | 39932.0419444444 | 2009-04-29 | 01:00:24
       0 | 20090429005954 | 40.009155 | 116.318091 |    0 | 265 | 39932.0420023148 | 2009-04-29 | 01:00:29
       0 | 20090429005954 | 40.009011 | 116.318092 |    0 | 265 | 39932.0420601852 | 2009-04-29 | 01:00:34
       0 | 20090429005954 | 40.008857 |  116.31812 |    0 | 261 | 39932.0421180556 | 2009-04-29 | 01:00:39
(10 rows)

编辑
使用 , CONCAT(gpsdate | | gpstime) 还会生成以下错误:

ERROR:  operator does not exist: | time without time zone
LINE 4:  , CONCAT(gpsdate | | gpstime)
                            ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
mnemlml8

mnemlml81#

我理解你的问题,你想产生一个 timestamp with time zonedate 和一个 time . 你可以用 + 操作员和附加铸件:

INSERT INTO trajectory (user_id, session_id, timestamp, lat, lon, alt)
SELECT 
    user_id + 1,
    session_id,
    (gpsdate + gpstime)::timestamp with time zone,
    lat,
    lon,
    alt
FROM tempo

相关问题