dongrao1862 2016-11-04 04:28
浏览 63

SQL结构和日历?

I'm having trouble unpacking a calendar structure from sql into golang structs, here is what I have.

type year struct {
year int
months []month
}

type month struct {
month int
days []day
}

type day struct {
day int
hours map[int]bool
}

I'm planning an appointment calendar, each day may have 10:00, 11:00, 12:00, 13:00 etc. and I will read out a maximum of 3 months at a time. I cannot figure out how to unpack the following schema:

CREATE TABLE appointments (
id INT,
year INT,
month INT,
day INT,
hour INT,
teacher INT, (id of teacher)
student INT, (id of student)
amount DECIMAL, (amount charged for the appointment)
booked BOOL, (records availability)
)

The query:

SELECT year, month, day, hour, booked FROM appointments a WHERE a.teacher = ? ORDERY BY day, month, year;

  • 写回答

1条回答 默认 最新

  • dqpc1845 2016-11-04 05:43
    关注

    In SQL Server you can create date tables and date time interval tables too easily.

    You can check how I implemented time interval table in SQL Server in referenced document.

    I joined a numbers table with 24 rows matching to hours and a second numbers table reference with 365 rows matching to dates in a year

    You need to convert below T-SQL to use it in your mySQL statements

    declare @date datetime = '20100101'
    
    SELECT
     dt.number+1 DayNo,
     tt.number+1 HourNo,
     dateadd(dd,dt.number,@date) [date],
     dateadd(hh,tt.number,dateadd(dd,dt.number,@date)) [start],
     dateadd(hh,tt.number+1,dateadd(dd,dt.number,@date)) [end]
    FROM master..spt_values dt, master..spt_values tt
    WHERE
     dt.Type = 'P' AND
     tt.Type = 'P' AND
     dt.number < 365 AND
     tt.number < 24 AND
     dateadd(dd,dt.number,@date) < dateadd(yy,1,@date)
    ORDER BY dt.Number, tt.Number
    
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂