我尝试为 iPhone 创建一个应用程序,您可以在其中设置约会。一切都保存在 MySQL 数据库中,我目前通过 JSON 将数据获取到我的应用程序中。这是一个工作流:
- 用户 1 定义他何时工作。例如。上午 8 点至下午 4 点。
- 用户 2 想与用户 1 有约会,例如早上 8 点到 9 点。
脚本应该能够做到这一点:
- 约会在用户的工作时间内;和
- 它不会与现有约会发生冲突,这可以通过三种可能的方式发生:
- 冲突约会在新约会期间开始;和/或
- 冲突约会在新约会期间结束;或
- 冲突约会在新约会之前开始并在新约会之后结束。
这些是重要的表格:
// new row should be added here when the conditions above are met
create table ios_appointment (
appointmentid int not null auto_increment,
start timestamp,
end timestamp,
user_id_fk int
)
// a working hour has a n:1 relationshipt to ios_worker
create table ios_workinghours (
workinghoursid int not null auto_increment,
start timestamp,
end timestamp,
worker_id_fk int
)
// employee, has a 1:n relationship to ios_workinghours
create table ios_worker (
workerid int not null auto_increment,
prename varchar(255),
lastname varchar(255),
...
)
select 子句的输入是两个时间戳,start
和 end
。这些由用户定义。因此脚本应该检查用户 2 在那个特定时间是否在工作以及是否已经有约会。
我目前有类似的东西,但它使用 user_id 链接表:
SELECT EXISTS (
SELECT *
FROM ios_appointments a JOIN ios_workhours h USING (user_id)
WHERE user_id = 1
AND h.start <= '08:00:00' AND h.end >= '09:00:00'
AND (
a.start BETWEEN '08:00:00' AND '09:00:00'
OR a.end BETWEEN '08:00:00' AND '09:00:00'
OR (a.start < '08:00:00' AND a.end > '09:00:00')
)
LIMIT 1
)
感谢您的帮助。谢谢。
最佳答案
您需要让您的应用读取数据并确定时间是否可用,或者您需要创建一个具有可用“时间段”(例如每 30 分钟)的 View 。
这是我的做法:
CREATE TABLE #timeslot
(
timeslot_id INT PRIMARY KEY IDENTITY(1,1),
timeslot_time DATETIME NOT NULL
)
DECLARE @startime DATETIME, @endtime DATETIME
SELECT @starttime = '12/25/2012 08:00:00.000', @endtime = '12/25/2012 15:00:00.000'
WHILE @starttime < @endtime BEGIN
INSERT INTO #timeslot (timeslot_time)
VALUES (@starttime)
SET @starttime = DATEADD(mm,30,@starttime)
END
SELECT
w.workerid,
ts.timeslot_time
INTO
ios_workertimeslot
FROM
#timeslot ts
FULL OUTER JOIN
ios_worker w
ON (1 = 1)
SELECT
wts.workerid,
wts.timeslot_time,
ap.appointmentid,
CASE WHEN ap.appointmentid IS NOT NULL THEN 0 ELSE 1 END AS AvailableSlot
FROM
ios_workertimeslot wts
JOIN
ios_workinghours wh
ON (wts.workerid = wh.workerid)
AND (wts.timeslot_time >= wh.start)
AND (wts.timeslot_time < wh.end)
LEFT JOIN
ios_appointment ap
ON (wts.workerid = ap.workerid)
AND (wts.timeslot_time >= ap.start)
AND (wts.timeslot_time < ap.end)
这将为您留下一个指示可用和不可用时隙的数据集。
希望这对您有所帮助!
关于iphone - 检测 iPhone 日历约会冲突的 SQL 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14029609/