如何从mySQL表格式化操作营业时间

时间:2012-10-04 03:56:57

标签: mysql asp-classic

看了可能已经有我答案的其他问题,我想为这个问题提供一些帮助。我有一个商业模板网站,其中大部分都有相同的开始和结束时间 - 周一。 - 周五,周末休息。但有些人可能会在星期五早些时候关闭,并在周六开放。我相信所有人都在周日休息。

我创建了一个表 -

CREATE TABLE `Shop_Hours` (
  `shop` mediumint(8) unsigned NOT NULL default '0',
  `d1s` time NOT NULL default '08:00:00',
  `d1e` time NOT NULL default '17:00:00',
  `d2s` time NOT NULL default '08:00:00',
  `d2e` time NOT NULL default '17:00:00',
.
.
.
  `d6s` time NOT NULL default '00:00:00',
  `d6e` time NOT NULL default '00:00:00',
  `d7s` time NOT NULL default '00:00:00',
  `d7e` time NOT NULL default '00:00:00'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

以下是一些记录:

INSERT INTO `Shop_Hours` (`shop`, `d1s`, `d1e`, `d2s`, `d2e`, `d3s`, `d3e`, `d4s`, `d4e`, `d5s`, `d5e`, `d6s`, `d6e`, `d7s`, `d7e`, `Comments`) VALUES(104, '08:00:00', '17:00:00', '08:00:00', '17:00:00', '08:00:00', '17:00:00', '08:00:00', '17:00:00', '08:00:00', '16:00:00', '08:00:00', '12:00:00', '00:00:00', '00:00:00', '');
INSERT INTO `Shop_Hours` (`shop`, `d1s`, `d1e`, `d2s`, `d2e`, `d3s`, `d3e`, `d4s`, `d4e`, `d5s`, `d5e`, `d6s`, `d6e`, `d7s`, `d7e`, `Comments`) VALUES(105, '08:00:00', '17:30:00', '08:00:00', '17:30:00', '08:00:00', '17:30:00', '08:00:00', '17:30:00', '08:00:00', '17:00:00', '00:00:00', '00:00:00', '00:00:00', '00:00:00', '');
INSERT INTO `Shop_Hours` (`shop`, `d1s`, `d1e`, `d2s`, `d2e`, `d3s`, `d3e`, `d4s`, `d4e`, `d5s`, `d5e`, `d6s`, `d6e`, `d7s`, `d7e`, `Comments`) VALUES(106, '08:30:00', '17:30:00', '08:30:00', '17:30:00', '08:30:00', '17:30:00', '08:30:00', '17:30:00', '08:30:00', '17:30:00', '08:30:00', '14:00:00', '00:00:00', '00:00:00', '');

我的问题涉及如何使用经典ASP查询记录并显示为以下某些示例:

周一 - 周五08:00 AM - 5:00 PM

周末休息

周一 - Th。上午8:00 - 下午5:00

周五 - 上午8:00 - 下午4:00

周六 - 上午8:00 - 下午12:00。

等等......

我认为有多种方法可以做到这一点我很想知道是否

  • 我的数据库结构还可以吗?
  • 获得目标最终结果的最有效方法是什么?

当然会对此有所帮助。

1 个答案:

答案 0 :(得分:0)

对您的架构的一些评论:

  1. 使用更好的变量名称。 'd1s',是周日还是周一? 'starttime_monday',也许?
  2. 默认值'00:00:00'表示'午夜'。也许默认值为NULL会更好。
  3. 我的回答第一件事就是将列翻转成行。您可以考虑以这种方式存储它们。
  4. 如果大多数公司的工作时间相同,那么为每个人存储价值都是低效的。仅存储例外情况。
  5. 也就是说,这是一个(缩小的)查询,它适用于您当前的架构。

    请注意,我使用的是DAYOFWEEK中的MySQL值,如果您在上面发表我的评论,则查询会发生变化。

    select shop,
      case when hours = "12:00AM - 12:00AM" then "Closed" else hours end as hours,
      min(weekday) as minday,
      max(weekday) as maxday
    from (
      select shop, "2" as weekday,
        concat(time_format(d1s,"%h:%i%p"),' - ',time_format(d1e,"%h:%i%p")) as hours
      from Shop_Hours
      union
      select shop, "3",
        concat(time_format(d2s,"%h:%i%p"),' - ',time_format(d2e,"%h:%i%p"))
      from Shop_Hours
      union
      select shop, "7",
        concat(time_format(d6s,"%h:%i%p"),' - ',time_format(d6e,"%h:%i%p"))
      from Shop_Hours
    ) foo
    group by 1,2;
    

    输出如下:

    +------+-------------------+--------+--------+
    | shop | hours             | minday | maxday |
    +------+-------------------+--------+--------+
    |  104 | 08:00AM - 05:00PM | 2      | 3      |
    |  104 | Closed            | 7      | 7      |
    +------+-------------------+--------+--------+
    

    这会给你留下日期的格式......

相关问题