为什么使用别名的时差不起作用?

问题描述 投票:-2回答:1
SELECT t.imei
     , t.date 
     , t.time startTime
     , t.ignition
     , t.tripStartStop
     , ( SELECT min(time) from gps_data where time>t.time and date>=t.date and imei='358480088853405' and tripStartStop=0 ) ' stopTime'
     , SUBTIME('startTime','stopTime') diff 
  from gps_data t 
 where imei='358480088853405' 
   and date>='2020-03-08' 
   and date<='2020-03-09' 
   and tripStartStop=1

上面的查询返回startTime和stopTime作为别名值,但我无法获得两者的差值

同时使用SUBTIME和TIMEDIFF函数

mysql
1个回答
1
投票

您不能使用这样的别名。查询:

SUBTIME('startTime','stopTime')

startTimestopTime作为字符串处理,因此将00:00:00处理为字符串。

您可以做的是以下事情:

select q.imei, q.date, q.startTime, q.ignition, 
       q.tripStartStop, q.stopTime, subtime(q.startTime, q.stopTime)
from (
  SELECT t.imei
       , t.date 
       , t.time startTime
       , t.ignition
       , t.tripStartStop
       , ( SELECT min(time) from gps_data where time>t.time and date>=t.date and imei='358480088853405' and tripStartStop=0 ) 'stopTime'
  from gps_data t 
  where 
    imei='358480088853405' 
     and date between '2020-03-08' and '2020-03-09' 
     and tripStartStop=1
) as q
© www.soinside.com 2019 - 2024. All rights reserved.