【问题标题】:Select every alternate days for selecting bus journey direction选择每隔一天选择巴士行车方向
【发布时间】:2011-12-13 12:34:39
【问题描述】:

我正在开发巴士票预订系统。在此,我为所有路线制作了表格,并在表格中添加了诸如 bus_number、fare 等字段。现在我们的公共汽车每隔一天就会在相反的方向行驶,即如果一辆公共汽车在 21-12-2011 从 X->Y 行驶,同一辆公共汽车将在下一个日期行驶 Y->X。那么如何获得公交车的方向呢?如果我为每个公共汽车服务提供商制作表格并添加公共汽车号码,并为某个日期添加一个类似“to”的标识符,我认为将有可能知道接下来几天的状态。我不知道这是不是一个好主意,所以请帮助我。

【问题讨论】:

    标签: php mysql date


    【解决方案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;
    

    【讨论】:

    • Tom 这个字段route_date date not null 将保留什么?
    • @SurajHazarika 我打算用它来保存巴士旅行的日期。我已经用一些示例数据和示例查询修改了我的答案,以尝试使其更清晰
    猜你喜欢
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    相关资源
    最近更新 更多