在MySQL中连接三个已完成的表

在MySQL中连接三个已完成的表,mysql,Mysql,我有三张桌子:tblFuel、tblDoXang、tblDrivingTime2。现在,我想在tblFuel中显示满足tblDrivingTime2中某些条件的fuelLevel字段,如果在tblFuel中时间戳上的特定时基中,如果我检查tblDoXang中是否有添加燃油操作,我必须在tblDoXang中插入ITNHIENLIUE字段以报告上面显示的fuelLevel。目标可能是: t燃油: tblDrivingTime2: stopTime 123456

我有三张桌子:tblFuel、tblDoXang、tblDrivingTime2。现在,我想在tblFuel中显示满足tblDrivingTime2中某些条件的fuelLevel字段,如果在tblFuel中时间戳上的特定时基中,如果我检查tblDoXang中是否有添加燃油操作,我必须在tblDoXang中插入ITNHIENLIUE字段以报告上面显示的fuelLevel。目标可能是: t燃油:

tblDrivingTime2:

 stopTime     
    123456         
    123478         
    123489
这将打印:

10
50
20  
现在我们检查一下 特布尔多桑

thoiGian      nhienLieu
123457        15
123466        10
它将插入上面的结果,最后,结果将是:

10
15
10
50
20
我编写了两个独立的查询来执行这些任务:

SELECT distinct from_unixtime(F.timestamp), F.fuelLevel FROM gtse.tblFuel F 
INNER JOIN gtse.tblDrivingTime2 D 
ON D.accountID = F.accountID and D.deviceID = F.deviceID 
where (from_unixtime(F.timestamp) between '2014-10-10 10:52:02' and '2014-10-30 10:52:02')
and F.accountID = 'vinhnghia'
and F.deviceID = '14C-00263'
and (D.reportType = '2' or D.reportType = '3')
and F.timestamp = D.stopTime
order by F.timestamp asc;
这将第一次打印结果10、50和20,如下所示:

SELECT distinct from_unixtime(D.thoiGian), D.nhienLieu
FROM gtse.tblDoXang D
inner join gtse.tblFuel F
on D.accountID = F.accountID and D.deviceID = F.deviceID
where D.accountID = 'vinhnghia' and D.deviceID = '14C-00263'
and D.thoiGian <= F.timestamp
order by D.thoiGian asc;

也许你可以试试工会条款:

( 
    SELECT distinct 
       from_unixtime(F.timestamp) As col_1, 
       F.fuelLevel as col_2
    FROM 
       gtse.tblFuel F 
       INNER JOIN gtse.tblDrivingTime2 D 
                  ON D.accountID = F.accountID 
                     and D.deviceID = F.deviceID
    where 
       (from_unixtime(F.timestamp) between '2014-10-10 10:52:02' and '2014-10-30 10:52:02') 
       and F.accountID = 'vinhnghia' 
       and F.deviceID = '14C-00263' 
       and (D.reportType = '2' or D.reportType = '3') 
       and F.timestamp = D.stopTime 
    order by 
       F.timestamp asc
)
UNION
(
    SELECT distinct 
       from_unixtime(D.thoiGian) as col_1, 
       D.nhienLieu as col_2 
    FROM 
       gtse.tblDoXang D 
       inner join gtse.tblFuel F 
                  on D.accountID = F.accountID 
                     and D.deviceID = F.deviceID 
    where 
       D.accountID = 'vinhnghia' 
       and D.deviceID = '14C-00263' 
       and D.thoiGian <= F.timestamp 
    order by 
       D.thoiGian asc;
)

我希望我理解得很好,这可能会有所帮助。

您的答案非常好,我尝试了,查询工作与我之前预期的一样正确。非常感谢。我真的很高兴它帮助了你:致以最良好的问候。
10
15
10
50
20
( 
    SELECT distinct 
       from_unixtime(F.timestamp) As col_1, 
       F.fuelLevel as col_2
    FROM 
       gtse.tblFuel F 
       INNER JOIN gtse.tblDrivingTime2 D 
                  ON D.accountID = F.accountID 
                     and D.deviceID = F.deviceID
    where 
       (from_unixtime(F.timestamp) between '2014-10-10 10:52:02' and '2014-10-30 10:52:02') 
       and F.accountID = 'vinhnghia' 
       and F.deviceID = '14C-00263' 
       and (D.reportType = '2' or D.reportType = '3') 
       and F.timestamp = D.stopTime 
    order by 
       F.timestamp asc
)
UNION
(
    SELECT distinct 
       from_unixtime(D.thoiGian) as col_1, 
       D.nhienLieu as col_2 
    FROM 
       gtse.tblDoXang D 
       inner join gtse.tblFuel F 
                  on D.accountID = F.accountID 
                     and D.deviceID = F.deviceID 
    where 
       D.accountID = 'vinhnghia' 
       and D.deviceID = '14C-00263' 
       and D.thoiGian <= F.timestamp 
    order by 
       D.thoiGian asc;
)