MySQL 如何获得两列之间的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3482033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:51:57  来源:igfitidea点击:

How to get difference between 2 columns

sqldatabasemysql

提问by StackOverflowNewbie

I have a query that is producing something like this:

我有一个查询,正在产生这样的东西:

StartTimestamp  |  EndTimestamp
================================
100             | 450
--------------------------------
150             | 500

I'd like the result to also include the difference between EndTimestamp and StartTimestamp:

我希望结果还包括 EndTimestamp 和 StartTimestamp 之间的差异:

StartTimestamp  |  EndTimestamp  |  Difference
==============================================
100             | 450            | 350
----------------------------------------------
150             | 600            | 450

How do I do this in MySQL?

我如何在 MySQL 中执行此操作?

回答by Alex Martelli

If the table is named, say, t:

如果表已命名,请说t

SELECT t.StartTimestamp, t.EndTimestamp, t.EndTimestamp - t.StartTimestamp AS Difference
FROM   &c

Of course, you don't need the t.parts in the select's columns if the undecorated names StartTimestampand EndTimestampare unambiguous in the context of the rest of your query.

当然,你不需要t.,如果未修饰的名称在选择的列的部分StartTimestampEndTimestamp在您的查询的其余部分的情况下毫不含糊。