我有 2 个表:
表 1:
|| *handtool_id* || *maintenance_interval_value* || *unit_unit_id* || *handtool_last_date_of_maintenance* || *handtool_next_date_of_maintenance* ||
|| 1 || 1 || 5 || 2014-11-07 || ||
|| 2 || 1 || 6 || 2014-11-07 || ||
|| 3 || 4 || 4 || 2014-11-07 || ||
表 2:
|| *unit_id* || *unit_name* || *unit_value* || *unit_parent_id* ||
|| 1 || Minute || 1 || 1 ||
|| 2 || Hour || 60 || 1 ||
|| 3 || Day || 1440 || 1 ||
|| 4 || Week || 10080 || 1 ||
|| 5 || Month || 32767 || 1 ||
|| 6 || Year || 525949 || 1 ||
根据*maintenance_interval_value*
和*unit_unit_id* + *handtool_last_date_of_maintenance*
计算*handtool_next_date_of_maintenance*
的正确语法是什么?
谢谢
最佳答案
正如 Mats Kindani 所建议的,您需要连接这两个表以获得下一个维护日期。
SELECT
table1.handtool_id,
table1.handtool_last_date_of_maintenance,
DATE_ADD(table1.handtool_last_date_of_maintenance,
INTERVAL table1.maintenance_interval_value*table2.unit_value SECOND) next_date_of_maintenance
FROM table1
JOIN table2 ON table1.unit_unit_id = table2.unit_id
我用来计算下一次维护日期的函数称为 DATE_ADD .您可以在上一个链接中阅读其用法。
最后,如果您想用“下一次维护日期”填充 table1
,则必须将我的选择查询转换为更新查询。
关于mysql - 如何从另一列和另一张表计算日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27203728/