简体   繁体   English

如何将列向量添加到 SQL 中的现有表?

[英]How to add a column vector to an existing table in SQL?

how can I add a column vector that I have created using SELECT (manipulating a column of the existing table) to that existing table?如何将使用 SELECT(操作现有表的列)创建的列向量添加到该现有表?

This is my starting matrix:这是我的起始矩阵:

name    date of Birth
Mark    15/01/1987
John    27/05/1945
Lisa    3/04/1981

I create with a SELECT and a function of the date their age:我用 SELECT 和他们年龄的日期函数创建:

Age
33
74
38

How can I create a new table (called table_new) in which I add the new column (Age) to the existing matrix formed by name and date of birth in SQL?如何创建一个新表(称为 table_new),在其中将新列(年龄)添加到由 SQL 中的姓名和出生日期形成的现有矩阵中? As follows:如下:

    name    date of Birth   Age
    Mark    15/01/1987   33
    John    27/05/1945   74
    Lisa    3/04/1981    38

I would recommend against storing that derived information in the table itself - ages do change over time, so maintaining the information will be tedious.我建议不要将派生信息存储在表本身中 - 年龄确实会随着时间的推移而变化,因此维护信息会很乏味。

You can, instead, create a view.相反,您可以创建一个视图。 In Postgres, you can use date function age() , which returns an interval that represents the date difference, and then extract its year part:在 Postgres 中,您可以使用日期函数age() ,它返回一个表示日期差异的interval ,然后提取其年份部分:

create view myview(name, dob, age) as 
select
    name,
    dob,
    extract(year from age(dob))
from mytable

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

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