2017-09-30 34 views
-1

我正在檢測時間衝突,但我被困在天衝突。這是我的表如何匹配mysql中結果中的單個字符?

INSERT INTO yourTable (starttime, endtime, day) 
VALUES 
    ('09:00:00', '11:00:00', 'MT'), 
    ('10:00:00', '12:00:00', 'M'), 
    ('06:00:00', '10:00:00', 'MTW'), 
    ('06:00:00', '10:00:00', 'ThFS'), 
    ('11:00:00', '12:00:00', 'S'), 
    ('15:00:00', '16:00:00', 'F'), 
    ('06:00:00', '09:00:00', 'M'); 

,這是查詢

SELECT DISTINCT 
    t1.starttime, 
    t1.endtime, 
    t1.day 
FROM yourTable t1 
LEFT JOIN yourTable t2 
    ON 
     (t1.starttime > t2.starttime AND t1.starttime < t2.endtime) OR 
     (t1.endtime > t2.starttime AND t1.endtime < t2.endtime) 
WHERE 
    t2.starttime IS NOT NULL; 

結果

starttime endtime day 
09:00:00 11:00:00 MT 
10:00:00 12:00:00 M 
06:00:00 10:00:00 MTW 
06:00:00 10:00:00 ThFS 
11:00:00 12:00:00 S 
06:00:00 09:00:00 M 

,應該是

starttime endtime day 
    09:00:00 11:00:00 MT 
    10:00:00 12:00:00 M 
    06:00:00 10:00:00 MTW 
    06:00:00 09:00:00 M 

我想MATCH AGAINST但並不好。謝謝您的幫助。 順便說一句:我嘗試使用LIKE,但仍然無法在MTW和TTh天。

+0

可怕的桌子設計!規範你的數據庫!解釋日可以包括多個平日,由他們的第一封信給您的聽衆!拆分爲每行單日,並使用週日數而不是週日名稱。添加一個ID字段(整數自動增量)作爲主鍵。然後,您可以在當天加入並過濾衝突。 – BitAccesser

+0

對不起,表格設計。我以某個地方爲例。謝謝你的想法。我會在成功後發佈更新。 – stoow

回答

0

已解決。感謝BitAccesser。我將日期轉換爲數字,然後通過php,我使用similar_text函數來比較結果。

相關問題