I am stuck on huge problem i will say with my below query. Here j5
represent friday and j6
represent saturday (1 to 7... sunday to monday).
As you know, the buses have different schedules depending on the time of the week. Here, I am taking next 5 trips departure after 25:00:00
on cal (j5)
and/or after 01:00:00
on cal2 (j6)
. Bus schedule are builded like this :
If it's 1 am then the current bus time is 25, 2 am is 26 ... you got it. So if I want departure trip for today after let's say 1 AM, i may get only 2-3 since the "bus" day end soon. To solve this problem, I want to add the next departure from the next day (here is saturday after friday). But next day start at 00 like every day in our world.
So what I want to do is : get all next trips for friday j5
after 25:00:00. If I don't have 5, then get all n trip departure for saturday after 01:00:00 (since 25:00:00 = 01:00:00).
Example : I get departure trip at 25:16:00, 25:46:00 and 26:16:00 for friday. It's 3. I want then to get 2 other departure trip for the next day so i get 5 at the end, and it will be like this 04:50:00 and 05:15:00. So next departure trip from X stop is : 25:16:00(friday), 25:46:00(friday), 26:16:00(friday), 04:50:00(saturday), 05:15:00(saturday).
I am having problem to sort both results from trips.trip_departure
.
I know it may be complicated, it's complicated for me to explain but... anyway. Got question I am here. Thanks a lot in advance !
PS: Using MySQL 5.1.49 and PHP 5.3.8 PS2: I want to avoid doing multiple query in PHP so I'd like to do this in one query, no matter what.
SELECT
trips.trip_departure,
trips.trip_arrival,
trips.trip_total_time,
trips.trip_direction
FROM
trips,
trips_assoc,
(
SELECT calendar_regular.cal_regular_id
FROM calendar_regular
WHERE calendar_regular.j5 = 1
) as cal,
(
SELECT calendar_regular.cal_regular_id
FROM calendar_regular
WHERE calendar_regular.j6 = 1
) as cal2
WHERE
trips.trip_id = trips_assoc.trip_id
AND
trips.route_id IN (109)
AND
trips.trip_direction IN (0)
AND
trips.trip_period_start <= "2011-11-25"
AND
trips.trip_period_end >= "2011-11-25"
AND
(
(
cal.cal_regular_id = trips_assoc.calendar_id
AND
trips.trip_departure >= "25:00:00"
)
OR
(
cal2.cal_regular_id = trips_assoc.calendar_id
AND
trips.trip_departure >= "01:00:00"
)
)
ORDER BY
trips.trip_departure ASC
LIMIT
5
EDIT Table structure :
Table calendar_regular
j1 mean sunday, j7 monday, etc).
`cal_regular_id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`j1` tinyint(1) NOT NULL COMMENT 'Lundi',
`j2` tinyint(1) NOT NULL COMMENT 'Mardi',
`j3` tinyint(1) NOT NULL COMMENT 'Mercredi',
`j4` tinyint(1) NOT NULL COMMENT 'Jeudi',
`j5` tinyint(1) NOT NULL COMMENT 'Vendredi',
`j6` tinyint(1) NOT NULL COMMENT 'Samedi',
`j7` tinyint(1) NOT NULL COMMENT 'Dimanche',
PRIMARY KEY (`cal_regular_id`),
KEY `j1` (`j1`),
KEY `j2` (`j2`),
KEY `j3` (`j3`),
KEY `j4` (`j4`),
KEY `j5` (`j5`),
KEY `j6` (`j6`),
KEY `j7` (`j7`)
Data :
cal_regular_id j1 j2 j3 j4 j5 j6 j7
1 0 0 0 0 1 0 0
2 0 0 0 1 1 0 0
3 1 1 1 1 1 0 0
4 0 0 0 0 0 1 0
5 0 0 0 0 0 0 1
Some bus are avaiable x days it's a table that define when in the week... assigned to the trip_assoc table.
Trips table
`agency_id` smallint(5) unsigned NOT NULL,
`trip_id` binary(16) NOT NULL,
`trip_period_start` date NOT NULL,
`trip_period_end` date NOT NULL,
`trip_direction` tinyint(1) unsigned NOT NULL,
`trip_departure` time NOT NULL,
`trip_arrival` time NOT NULL,
`trip_total_time` mediumint(8) NOT NULL,
`trip_terminus` mediumint(8) NOT NULL,
`route_id` mediumint(8) NOT NULL,
`shape_id` binary(16) NOT NULL,
`block` binary(16) DEFAULT NULL,
KEY `testing` (`route_id`,`trip_direction`),
KEY `trip_departure` (`trip_departure`)
trips_assoc table
`agency_id` tinyint(4) NOT NULL,
`trip_id` binary(16) NOT NULL,
`calendar_id` smallint(6) NOT NULL,
KEY `agency_id` (`agency_id`),
KEY `trip_id` (`trip_id`,`calendar_id`)