简体   繁体   English

如何计算 PostgreSQL 中的列

[英]How to calculate column in PostgreSQL

I have a table that contains the id, name and age of a person as an int, for instance 20. I have to create a select that gets the name, age, and year of birth.我有一个表,其中包含一个人的 id、姓名和年龄作为 int,例如 20。我必须创建一个 select 来获取姓名、年龄和出生年份。 How can I calculate this?我该如何计算?

So far I have this, but it doesn't work:到目前为止,我有这个,但它不起作用:

SELECT name, age, CURRENT_YEAR-age as yearOfBirth
FROM person;

You can do:你可以做:

SELECT 
  name, 
  age, 
  extract(year from current_date) - age as yearOfBirth
FROM person;

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

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