2012-10-04 41 views
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時間相交,該球員在設定的結果所示!!

+0

是否「DELETE FROM不table_name where id(not)in「是否符合你的期望? – wojciechz

+0

我不想從表格中刪除,我只想從結果集中排除指定時間內忙碌的玩家。 – Ayman

+0

好吧,據我所知,你想否定你的選擇權?爲什麼不選擇從table_name的東西其中ID不在(您的數據) – wojciechz

回答

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 
    ) 
)