簡體   English   中英

從 select table2 插入 table1

[英]Inserting into table1 from select table2

我有以下兩個表:

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. 將 1 添加到user_id
  2. gpsdategpstime字段從tempo連接到trajectory中的timestamp字段。

詢問:

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.

據我了解您的問題,您想從datetime生成timestamp with time zone 您可以使用+運算符和其他轉換來執行此操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM