I have database that is used to manage sport academy. Now I have table for players, class and class_player (that join the other two tables). What I want to do is when enrolling players to classA, the players which are enrolled to classB (which intersect with classA time) are removed from the result-set of the select statement
Player Table: class Table class_player Table
ID INT ID INT classID INT
timeFrom TIME playerID INT
timeTo TIME
days BIT(7)
In "class" Table: timeFrom: is the begining of the class time
timeTo: is the end of the class time
days: holds the days that the class is teached,
ex: 0x000001 -> one of the seven days will include this class
The select statement I use to get the players:
(
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`)
);
The problem is when a player have two classes one that does and one that doesn't intersect with classA time, this player is shown in the result-set!!
First: thanks to wojciechz for his efforts, the solution was in his second answer but i didn't understand it until my friend told me a similar solution.
Second: The solution in the SQL will be:
1. get all the players who are busy
2. then search for the player who are not in this list
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
)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.