我有3个表timepart,schedule,schedule_daypart。表格中的数据如下,
Daypart表有4个条目
id | start_time | end_time
1 | 09:00:01 | 12:00:00
2 | 12:00:01 | 18:00:00
3 | 18:00:01 | 23:59:59
4 | 00:00:01 | 09:00:00
Schedule table
id | start_date | end_date
1 | 10-Sept-2013 | 12-Sept-2013
2 | 10-Sept-2013 | 15-Sept-2013
schedule_daypart
id | schedule_id | daypart_id
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 1 | 4
5 | 2 | 1
6 | 2 | 2
7 | 2 | 3
现在我想编写存储过程,它将获取日期和时间参数,并给出schedule_id和daypart_id。例如如果我提供日期为11-Sett-2013,时间为13:30:00,则输出将为
schedule_id | daypart_id
1 | 2
1 | 3
1 | 4
2 | 2
2 | 3
答案 0 :(得分:0)
试试这个:
SELECT
JT.schedule_id, JT.daypart_id
FROM schedule_daypart JT
INNER JOIN daypart DP ON JT.daypart_id = DP.id
INNER JOIN Schedule S ON JT.schedule_id = S.id
WHERE S.start_date <= STR_TO_DATE('2013, 11 09', '%Y, %d %m') AND S.end_date >= STR_TO_DATE('2013, 11 09', '%Y, %d %m') AND DP.start_time >= STR_TO_DATE('13:30:00','%h:%i:%s') AND DP.end_time <= STR_TO_DATE('13:30:00','%h:%i:%s')
更新:
STR_TO_DATE('13:30:00','%h:%i:%s')