I want to display the subtraction of two values from two different columns to a third column using a SQL query.
This is the table structure:
------------------------------------
id | subject | Time1 | Time2| Time3
------------------------------------
1 | String1 | 50 | 78 |
2 | String2 | 60 | 99 |
3 | | | |
I want to subtract Time2 from Time1 if both Time1 and Time2 has data and store in Time3.
Output should be like as
------------------------------------
id | subject | Time1 | Time2| Time3
------------------------------------
1 | String1 | 50 | 78 | 28
2 | String2 | 60 | 99 | 39
3 | | | |
Thanks!
解决方案`SELECT
id,
subject,
Time1,
Time2,
IF( Time1 != '' AND Time2 != '', Time2-Time1, '') as Time3
FROM tbl_time`
博客围绕SQL查询展开,给出表结构,包含id、subject、Time1、Time2和Time3列。需求是当Time1和Time2都有数据时,用Time2减去Time1,并将结果存储在Time3列。最后提供了实现该功能的SQL解决方案。
215

被折叠的 条评论
为什么被折叠?



