gpt4 book ai didi

PostgreSQL 从日期类型计算年龄

转载 作者:行者123 更新时间:2023-11-29 11:37:50 28 4
gpt4 key购买 nike

我有下表:

CREATE TABLE public.employees
(
employee_id integer NOT NULL,
name text NOT NULL,
date_of_birth date,
address text,
email text,
CONSTRAINT employees_pkey PRIMARY KEY (employees_id),
CONSTRAINT employees_email_key UNIQUE (email)
)

我如何才能在输出中列出每位员工的姓名及其年龄?

谢谢。

最佳答案

使用 date_part() 和 age() 函数

SELECT name text, date_part('year',age(date_of_birth)),* FROM public.employees

有关日期函数的文档,请参阅 https://www.postgresql.org/docs/current/static/functions-datetime.html

关于PostgreSQL 从日期类型计算年龄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40072914/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com