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`