2011-12-13 77 views
0

我正在使用公交車票預訂系統。在此,我爲所有路線製作了表格,並在表格中添加了bus_number,票價等字段。現在,我們的公共汽車每隔一天往相反的方向行駛,也就是說,如果一輛公共汽車從2011年12月21日的X-> Y行駛,則同一輛公共汽車將在下一個日期前往Y-> X。那麼怎樣才能爲公交車定向呢?如果我爲每個巴士服務提供商製作了表格並添加巴士號碼,那麼在日期中添加一個類似'to'的標識符,我認爲有可能知道下一天的狀態。我不知道它是否是一個好主意,所以請幫助我。選擇每隔一天選擇公交車路線方向

回答

1

不知道現有表格的確切細節,提供確定的解決方案有點困難。總之,這裏的如何,你可以與他們站和票價沿着持有巴士的建議:

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; 
+0

湯姆這個字段'route_date'日期不爲空將保存? –

+0

@SurajHazarika我打算保留巴士旅程的日期。我已經用一些示例數據和一個示例查詢修改了我的答案,嘗試使其更清晰 –