如何从三张桌子中获取价值?

时间:2014-09-29 07:22:58

标签: mysql database

我有三张这样的表:

-drivingtime
  +id
  +starttime
  +stoptime
-fuel
  +id
  +timestamp
  +fuellevel
-addingfuel
  +id
  +timestamp
  +fueladding

我希望在同一个屏幕上显示启动时间,停止时间,燃油液位(启动时间),燃油液位(停止时间)。那我到底要做什么呢?我搜索谷歌的解决方案,但其他问题是完全不同的。

我试过了:

SELECT startTime and stopTime 
from tblDrivingTime d 
left join tblFuel f on d.startTime = f.timestamp 
left join tblFuel f on d.stopTime = f.timestamp 
left join tblDoXang x on d.startTime < x.timestamp < d.stopTime

1 个答案:

答案 0 :(得分:1)

这是一个返回您要查找的结果的查询:

SELECT DT.starttime
    ,DT.stoptime
    ,FB.fuellevel AS `startfuellevel`
    ,FE.fuellevel AS `stopfuellevel`
    ,(SELECT COUNT(AF.id)
     FROM addingfuel AF
     WHERE AF.timestamp BETWEEN DT.starttime AND DT.stoptime) AS `nbaddingevents
FROM drivingtime DT
INNER JOIN fuel FB ON FB.timestamp = DT.starttime
INNER JOIN fuel FE ON FE.timestamp = DT.stoptime

希望这会对你有所帮助。