简体   繁体   English

尝试根据 mySQL 中 SELECT 语句的结果更改表

[英]trying to ALTER a table based on the results from a SELECT statement in mySQL

when i use the following select statement i can return the date differences between date_due and date_returned.当我使用以下选择语句时,我可以返回 date_due 和 date_returned 之间的日期差异。

select Date_Due,Date_Returned,
datediff(Date_Due,Date_Returned) as Day_Diff 
From tp_rental

However i cant get these values to replace the current null values in the tp_rental table.但是我无法获得这些值来替换 tp_rental 表中的当前空值。 I tried the below statement also thinking it would create a new column.我尝试了下面的语句,也认为它会创建一个新列。

alter table tp_rental
ADD DayDiff as DATEDIFF (Date_Due,Date_Returned);

my errors say there is a problem with the syntax near as DATEDIFF我的错误说 DATEDIFF 附近的语法有问题

any ideas?有任何想法吗? thanks in advance提前致谢

Generated column expressions need to be enclosed in () (see the manual ).生成的列表达式需要用()括起来(参见手册)。 You also need to add a column type definition:您还需要添加列类型定义:

alter table tp_rental
ADD DayDiff INT as (DATEDIFF(Date_Due,Date_Returned));

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

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