psql:复制到表问题

时间:2017-08-09 07:55:47

标签: sql postgresql psql

说我有users.data包含...

Jeff    31  2
Niel    42  15
Chucky  42  15
Paul    48  15
Bert    18  0
Geoff   28  16
Kieth   64  31
Clyde   20  0
Greg    44  3
Bonnie  19  0
Meyer   34  6
Alice   41  3

在psql中,我创建了一个像这样的用户表......

ID |  name | age | number
---+-------+-----+----------
1    Jeff     31     2
2    Niel     42    15
3    Chucky   42    15
4    Paul     48    15
5    Bert     18     0
6    Geoff    28    16
7    Kieth    64    31
8    Clyde    20     0
9    Greg     44     3
10   Bonnie   19     0
11   Meyer    34     6
12   Alice    41     3

然后我有events.data包含...

Jeff    Party   Newtown 2012-02-16
Jeff    Work    Oldtown 2005-03-30
Chucky  Work    Oldtown 2008-11-09
Niel    Work    Newtown 2005-02-28
Paul    Party   Newtown 2010-02-14
Bert    Work    Oldtown 2005-03-30
Geoff   Work    Newtown 2013-04-20
Kieth   Sleep   Oldtown 2009-08-03
Clyde   Party   Newtown 2012-02-16
Greg    Work    Newtown 2013-04-20
Bonnie  Sleep   Newtown 2009-08-30
Meyer   Sleep   Newtown 2008-10-30
Alice   Work    Newtown 2012-03-30

如何创建表格,事件,像这样?

ID | user_ID | event |   city        date
---+---------+-------+----------+--------------
1      1       Party    Newtown     2012-02-16
2      1       Work     Oldtown     2005-03-30
3      2       Work     Oldtown     2008-11-09
4      3       Work     Newtown     2005-02-28
5      4       Party    Newtown     2010-02-14
6      5       Work     Oldtown     2005-03-30
7      6       Work     Newtown     2013-04-20
8      7       Sleep    Oldtown     2009-08-03
9      8       Party    Newtown     2012-02-16
10     9       Work     Newtown     2013-04-20
11    10       Sleep    Newtown     2009-08-30
12    11       Sleep    Newtown     2008-10-30
13    12       Work     Newtown     2012-03-30

基本上,当我填充我创建的表时,当我要将数据复制到文件时,我需要找到匹配事件记录的用户名属性的用户的id。因此,当您查看事件表时,而不是显示名称,而是显示同名用户的ID。

我该怎么做呢?当我使用时,我能做到这一点......

COPY events (user_ID, event, city, date) FROM :dir || 'events.data' WITH (/* ??? */);

0 个答案:

没有答案