简体   繁体   中英

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. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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