我有一个数据库,其中包含表Plane,表Flight,这基本上是飞机执行的Flight,然后对于每个航班,我都解决了M:N表,该表仅包含Flight和Destination的外键,表中有2个条目Destination,要了解Departure和Arrival之间的差异,表Destination与表Type(Departure and Arrival,在另一个表中设置,该行有2行,“ Zacetna” = Departure,“ Koncna” = Arrival)相关联。表目标还具有到表日期的连接,该表包含日期时间。
我想减去每个航班的到达和离开时间,然后求和飞机是否有多个航班。
我试图先将它们全部相减然后再相减,但是那没有用,然后我试图进行分组依据,但是没有运气。
在所附图片中。 名为“ Falcon FX 3000”的飞机有2个航班,分别是idFlight 5和6。要获得预期的结果,我将不得不这样做
( Destination_6_Date - Destination_5_Date) + (Destination_4_Date - Destination_7_Date)
但我的解决方案都无效
SELECT d.Ime, letD.Destinacija_idDestinacija, l.idLet,tip.Tip, t.Termin
FROM Letalo p
INNER JOIN Let l ON l.Letalo_idLetalo = p.idLetalo
INNER JOIN Let_Has_Destinacija letD on letD.Let_idLet = l.idLet
INNER JOIN Destinacija d on d.idDestinacija = letD.Destinacija_idDestinacija
INNER JOIN Termin t on t.idTermin = d.Termin_idTermin
INNER JOIN Tip_Destinacije tip on tip.idTip_Destinacije = Tip_Destinacije_idTip_Destinacije WHERE p.Naziv='Falcon FX 3000';
https://i.imgur.com/5r2G9yI.png
https://i.imgur.com/3CIfbUN.png
Sorry for the ERDiagram for being in the different language
Letalo = Plane
Let = Flight
Let_Has_Destinacija = Solved M:N, holding Flight ID and Destination ID
Destinacija = Destination
Termin = Date
Tip_Destinacija = Type
编辑 这是成功的GROUP BY查询,它返回有效的计算,现在我只需要对它们进行求和。我猜想这将通过子查询完成,但是我不太了解,因为我是MySQL的新手
SELECT d.Ime as Name, letD.Destinacija_idDestinacija as Destination, l.idLet as idFlight,tip.Tip as Type, CAST(t.Termin as DATE), timediff(Max(t.Termin), min(t.Termin))
FROM Letalo p
INNER JOIN Let l ON l.Letalo_idLetalo = p.idLetalo
INNER JOIN Let_Has_Destinacija letD on letD.Let_idLet = l.idLet
INNER JOIN Destinacija d on d.idDestinacija = letD.Destinacija_idDestinacija
INNER JOIN Termin t on t.idTermin = d.Termin_idTermin
INNER JOIN Tip_Destinacije tip on tip.idTip_Destinacije = Tip_Destinacije_idTip_Destinacije WHERE p.Naziv='Falcon FX 3000'
GROUP BY l.idLet;
https://i.imgur.com/hUXZDnQ.png
编辑2 通过使用临时表,我设法求出了飞行时间,但是我必须对此使用两个查询,这不是我想要的。
DROP TABLE IF EXISTS tempTime;
CREATE TEMPORARY TABLE tempTime (
SELECT timediff(Max(t.Termin), min(t.Termin)) as FlightTime
FROM Letalo p
INNER JOIN Let l ON l.Letalo_idLetalo = p.idLetalo
INNER JOIN Let_Has_Destinacija letD on letD.Let_idLet = l.idLet
INNER JOIN Destinacija d on d.idDestinacija = letD.Destinacija_idDestinacija
INNER JOIN Termin t on t.idTermin = d.Termin_idTermin
INNER JOIN Tip_Destinacije tip on tip.idTip_Destinacije = Tip_Destinacije_idTip_Destinacije WHERE p.Naziv='Falcon FX 3000'
GROUP BY l.idLet);
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(FlightTime))) as FlownTime FROM tempTime;
DROP TABLE IF EXISTS tempTime;
https://i.imgur.com/HlPtgHr.png
接受的答案
SELECT
`Name`,
`Destination`,
`Type`,
`Date`,
SEC_TO_TIME(SUM(TIME_TO_SEC(`Time_diff`))) as `Tot_Diff`
FROM (
SELECT
d.`Ime` as `Name`,
letD.`Destinacija_idDestinacija` as `Destination`,
l.`idLet` as idFlight,tip.`Tip` as `Type`,
CAST(t.`Termin` as DATE) as `Date`,
timediff(Max(t.`Termin`), min(t.`Termin`)) as `Time_diff`
FROM Letalo p
INNER JOIN `Let` l
ON l.`Letalo_idLetalo` = p.`idLetalo`
INNER JOIN `Let_Has_Destinacija` letD
on letD.`Let_idLet` = l.`idLet`
INNER JOIN `Destinacija` d
on d.`idDestinacija` = letD.`Destinacija_idDestinacija`
INNER JOIN `Termin` t
on t.`idTermin` = d.`Termin_idTermin`
INNER JOIN `Tip_Destinacije` tip
on tip.`idTip_Destinacije` = `Tip_Destinacije_idTip_Destinacije`
WHERE p.Naziv = 'Falcon FX 3000'
GROUP BY l.idLet
) as `main`
答案 0 :(得分:0)
我相信这应该产生您描述的结果。基本上,它接受您拥有的查询,并将其用作子查询 然后将时间差求和。
SELECT
`Name`,
`Destination`,
`Type`,
`Date`,
SEC_TO_TIME(SUM(TIME_TO_SEC(`Time_diff`))) as `Tot_Diff`
FROM (
SELECT
d.`Ime` as `Name`,
letD.`Destinacija_idDestinacija` as `Destination`,
l.`idLet` as idFlight,tip.`Tip` as `Type`,
CAST(t.`Termin` as DATE) as `Date`,
timediff(Max(t.`Termin`), min(t.`Termin`)) as `Time_diff`
FROM Letalo p
INNER JOIN `Let` l
ON l.`Letalo_idLetalo` = p.`idLetalo`
INNER JOIN `Let_Has_Destinacija` letD
on letD.`Let_idLet` = l.`idLet`
INNER JOIN `Destinacija` d
on d.`idDestinacija` = letD.`Destinacija_idDestinacija`
INNER JOIN `Termin` t
on t.`idTermin` = d.`Termin_idTermin`
INNER JOIN `Tip_Destinacije` tip
on tip.`idTip_Destinacije` = `Tip_Destinacije_idTip_Destinacije`
WHERE p.Naziv = 'Falcon FX 3000'
GROUP BY l.idLet
) as `main`
GROUP BY `Name`,`Destination`,`Type`,`Date`;