如何使用SQL查询在表中的两个字段之间进行减法 [英] How to do subtraction between two fields in a Table using an SQL query
本文介绍了如何使用SQL查询在表中的两个字段之间进行减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用SQL查询显示两个不同的值从两个不同的列减去到第三列.
I want to display the subtraction of two values from two different columns to a third column using a SQL query.
这是表结构:
------------------------------------
id | subject | Time1 | Time2| Time3
------------------------------------
1 | String1 | 50 | 78 |
2 | String2 | 60 | 99 |
3 | | | |
如果Time1和Time2都具有数据并存储在Time3中,我想从Time1中减去Time2.
I want to subtract Time2 from Time1 if both Time1 and Time2 has data and store in Time3.
输出应该像
------------------------------------
id | subject | Time1 | Time2| Time3
------------------------------------
1 | String1 | 50 | 78 | 28
2 | String2 | 60 | 99 | 39
3 | | | |
谢谢!
推荐答案
`SELECT
id,
subject,
Time1,
Time2,
IF( Time1 != '' AND Time2 != '', Time2-Time1, '') as Time3
FROM tbl_time`
这篇关于如何使用SQL查询在表中的两个字段之间进行减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文