1
我有用於管理體育學院的數據庫。現在我有玩家,班級和class_player(加入另外兩張桌子)的桌子。招收球員CLASSA當我想要做的是,這是就讀於CLASSB(與ClassA的時間相交)的玩家SELECT語句如何消除MySQL中不符合特定條件的行
Player Table: class Table class_player Table
ID INT ID INT classID INT
timeFrom TIME playerID INT
timeTo TIME
days BIT(7)
在「類」表的結果集刪除: timeFrom:是的上課時間
timeTo開始時:是的上課時間
天結束:持有天這個類TEACHED,
例如:0x000001 - >的七天一個包括這個類
我使用select語句來獲得球員:
(
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`)
);
問題是,當一個玩家有兩個類一個沒有和一個不與CLASSA時間相交,該球員在設定的結果所示!!
是否「DELETE FROM不table_name where id(not)in「是否符合你的期望? – wojciechz
我不想從表格中刪除,我只想從結果集中排除指定時間內忙碌的玩家。 – Ayman
好吧,據我所知,你想否定你的選擇權?爲什麼不選擇從table_name的東西其中ID不在(您的數據) – wojciechz