我有用于管理体育学院的数据库。现在我有玩家,类和class_player的表(加入其他两个表)。我想要做的是当将玩家注册到classA时,注册到classB的玩家(与classA时间相交)将从select语句的结果集中删除
Player Table: class Table class_player Table
ID INT ID INT classID INT
timeFrom TIME playerID INT
timeTo TIME
days BIT(7)
In" class"表:
timeFrom:是上课时间的开始
timeTo:是上课时间的结束
days:掌握上课的日子,
例如:0x000001 - >七天中的一天将包括这个课程
我用来获取玩家的选择语句:
(
SELECT `players`.`ID`
FROM `players` JOIN `class_player` ON `players`.`ID` = `class_player`.`playerID`
JOIN `classes` ON `class_player`.`classID` = `classes`.`ID`
WHERE (
-- Get all player with no day intersection
-- 1 is a variable of the entered day ex: 0x00100100
NOT (`classes`.`days` & 1)
-- Time Intersection
OR (
-- Entered "FROM" != Existing "FROM"
'10:00:00' != `timeFrom`
-- Entered "TO" != Existing "TO"
AND '13:00:00' != `timeTo`
-- Entered "FROM" Not Between Existing (FROM, TO)
AND NOT ('10:00:00' > `timeFrom` AND '10:00:00' < `timeTo`)
-- AND '10:00:00' NOT BETWEEN `timeFrom` AND `timeTo`
-- Entered "TO" Not Between Existing (FROM, TO)
AND NOT ('13:00:00' < `timeTo` AND '13:00:00' > `timeFrom`)
-- AND '13:00:00' NOT BETWEEN `timeFrom` AND `timeTo`
-- Entered "FROM" Not Less than Existing "FROM"
-- Entered "TO" Not Bigger than Existing "TO"
AND NOT ('10:00:00' < `timeFrom` AND '13:00:00' > `timeTo` )
)
)
)
UNION
(
-- Players who don't have any classes
SELECT `players`.`ID`
FROM `players`
WHERE `players`.`ID` NOT IN (SELECT `playerID` FROM `class_player`)
);
问题是当一个玩家有两个班级,一个班级与班级时间不相交时,这个玩家会在结果集中显示!!
答案 0 :(得分:0)
首先:感谢wojciechz他的努力,解决方案是他的第二个答案,但我不理解,直到我的朋友告诉我类似的解决方案。
第二:SQL中的解决方案是:
1.让所有忙碌的球员得到
2.然后搜索不在此列表中的玩家
SELECT DISTINCT `ID` FROM `players` WHERE `ID` NOT IN (
(
SELECT `players`.`ID`
FROM `players` JOIN `class_player` ON `players`.`ID` = `class_player`.`playerID`
JOIN `classes` ON `class_player`.`classID` = `classes`.`ID`
WHERE (
-- Get all player with no day intersection
-- 1 is a variable of the entered day ex: 0x00100100
(`classes`.`days` & 1)
-- Time Intersection
AND (
-- Entered "FROM" != Existed "FROM"
'10:00:00' = `timeFrom`
-- Entered "TO" != Existed "TO"
OR '13:00:00' = `timeTo`
-- Entered "FROM" Not Between Existing (FROM, TO)
OR ('10:00:00' > `timeFrom` AND '10:00:00' < `timeTo`)
-- AND '10:00:00' NOT BETWEEN `timeFrom` AND `timeTo`
-- Entered "TO" Not Between Existing (FROM, TO)
OR ('13:00:00' < `timeTo` AND '13:00:00' > `timeFrom`)
-- AND '13:00:00' NOT BETWEEN `timeFrom` AND `timeTo`
-- Entered "FROM" Not Less than Existing "FROM"
-- Entered "TO" Not Bigger than Existing "TO"
OR ('10:00:00' < `timeFrom` AND '13:00:00' > `timeTo` )
)
)
)
UNION
(
-- ANOTHER DATE AND TIME
)
)