这里是我的sql。
create table student(
id smallint primary key auto_increment,
class_id smallint not null ,
name varchar(10) not null ,
birthday date not null ,
sex bool not null ,
age int as (to_days(now()-birthday))
[2022-04-10 12:08:42] [HY000][3763] Expression of generated column 'age' contains a disallowed function: now.
[2022-04-10 12:08:42] [HY000][3763] Expression of generated column 'age' contains a disallowed function: now.
我搜索了一下资料,了解到像now()
这样返回值不确定的函数不能用于计算列表达式。我应该如何实现生日列的计算?
在创建表时不要使用
列。你可以在查询时找到年龄,如下图所示。
age
SELECT TIMESTAMPDIFF(YEAR, birthday ,CURDATE()) as age FROM student
你也可以查看这个问题和答案
是的,你可以通过使用MySQL生成的列来做到这一点,所以你必须重新创建学生表,如下所示。