MySql 中计算列的条件值
我正在尝试计算两个日期列之间的差异,其工作原理如下:
select (ship_date - due_date) AS DaysTaken;
基本上,我想确定订单是否按时发货,如果没有,则晚了多少天。正如预期的那样,这工作正常,但如果订单提前发货,我会得到一个负数。我想做的是评估结果,如果计算出负值,则返回 0。
我不知道如何在 MySQL select 语句中执行此操作。有人可以帮忙吗?
I'm trying to calculate the difference between two date columns, which works just fine as:
select (ship_date - due_date) AS DaysTaken;
Basically, I want to determine if orders were shipped on time and, if not, how many days late. This works fine, as expected, except, if the order is shipped early, I get a negative number. What I'd like to do is to evaluate the results and return 0, if it calculates a negative value.
I don't know how to do this in a MySQL select statement. Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 GREATEST() 函数将负数替换为 0:
You can use the GREATEST() function to replace negative numbers with 0: