Не зная точных деталей ваших существующих таблиц, немного сложно найти окончательное решение.В любом случае, вот предложение о том, как вы можете держать автобусы вместе с их остановками и тарифами:
CREATE TABLE `bus` (
`id` int unsigned not null primary key auto_increment,
`bus_number` varchar(55) not null,
UNIQUE KEY `busUidx1` (`bus_number`)
) ENGINE=InnoDB;
CREATE TABLE `bus_stop` (
`id` int unsigned not null primary key auto_increment,
`stop_description` varchar(250) not null,
UNIQUE KEY `bus_stopUidx1` (`stop_description`)
) ENGINE=InnoDB;
CREATE TABLE `bus_route` (
`id` int unsigned not null primary key auto_increment,
`bus_id` int unsigned not null,
`route_date` date not null,
`bus_start_stop_id` int unsigned not null,
`bus_end_stop_id` int unsigned not null,
`fare` decimal (10,2) not null,
UNIQUE KEY `bus_stopUidx1` (`bus_id`,`route_date`),
CONSTRAINT `fk_bus_route_bus_fk1` FOREIGN KEY (`bus_id`) REFERENCES `bus` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_bus_route_stop_fk1` FOREIGN KEY (`bus_start_stop_id`) REFERENCES `bus_stop` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_bus_route_stop_fk2` FOREIGN KEY (`bus_end_stop_id`) REFERENCES `bus_stop` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;
Используя эту модель, вы сможете хранить список автобусов (в таблице bus
),список всех возможных остановок (в таблице bus_stop
) и автобусных маршрутов на указанную дату.Это также позволит вам гибко нарушать правило «путешествия на автобусе из X> Y, а затем правило всегда возвращается из Y> X», которое, если автобусы, на которых я ездил в прошлом, являются чем-тоПойдемте, может оказаться полезным; -)
РЕДАКТИРОВАТЬ
Итак, вот несколько примеров данных, чтобы попытаться проиллюстрировать мой ответ далее:
insert into bus (bus_number) values ('Red Bus 1');
insert into bus (bus_number) values ('Red Bus 2');
insert into bus (bus_number) values ('Yellow Bus 1');
insert into bus (bus_number) values ('Yellow Bus 2');
insert into bus_stop (stop_description) values ('Stop 1');
insert into bus_stop (stop_description) values ('Stop 2');
insert into bus_stop (stop_description) values ('Stop 3');
insert into bus_stop (stop_description) values ('Stop 4');
insert into bus_route (bus_id,route_date,bus_start_stop_id,bus_end_stop_id,fare)
values (
(select id from bus where bus_number = 'Red Bus 1'),
'2011-12-11',
(select id from bus_stop where stop_description = 'Stop 1'),
(select id from bus_stop where stop_description = 'Stop 2'),
3.45);
insert into bus_route (bus_id,route_date,bus_start_stop_id,bus_end_stop_id,fare)
values (
(select id from bus where bus_number = 'Red Bus 1'),
'2011-12-12',
(select id from bus_stop where stop_description = 'Stop 2'),
(select id from bus_stop where stop_description = 'Stop 1'),
3.45);
insert into bus_route (bus_id,route_date,bus_start_stop_id,bus_end_stop_id,fare)
values (
(select id from bus where bus_number = 'Yellow Bus 1'),
'2011-12-11',
(select id from bus_stop where stop_description = 'Stop 3'),
(select id from bus_stop where stop_description = 'Stop 4'),
1.95);
insert into bus_route (bus_id,route_date,bus_start_stop_id,bus_end_stop_id,fare)
values (
(select id from bus where bus_number = 'Yellow Bus 1'),
'2011-12-12',
(select id from bus_stop where stop_description = 'Stop 4'),
(select id from bus_stop where stop_description = 'Stop 3'),
1.95);
И, наконец, запрос ксоедините столы вместе:
select b.bus_number,
br.route_date,
bs.stop_description as start,
be.stop_description as end,
br.fare
from bus_route br
inner join bus b on b.id = br.bus_id
inner join bus_stop bs on bs.id = br.bus_start_stop_id
inner join bus_stop be on be.id = br.bus_end_stop_id;