我有三个表:tblFuel,tblDoXang,tblDrivingTime2。现在我想在tblFuel中显示满足tblDrivingTime2中某些条件的fuelLevel字段,如果在特定时间(基于tblFuel中的时间戳),如果我检查在tblDoXang中添加燃料操作,我必须插入它(nhienLieu字段在tblDoXang )报告显示上面的fuelLevel。目标很可能是: tblFuel:
timestamp fuelLevel
123456 10
123467 8
123478 50
123489 20
tblDrivingTime2:
stopTime
123456
123478
123489
这将打印:
10
50
20
现在我们检查是否 tblDoXang
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;
将显示值(15,10)。我的问题是如何加入两个查询以显示最终结果?:
10
15
10
50
20
答案 0 :(得分:1)
也许您可以尝试使用UNION子句:
(
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;
)
我希望我理解得很好,这可能会有所帮助。