2016-02-15 274 views
-2

實現循環我有我的SQL Server數據庫FACULTY_RECORDS & SCHOOL_RECORDS兩個表。我有一個包含約他們在表FACULTY_RECORDS了講座,幷包含由學校分配給院系對他們的主題進行演講時隙行院系填寫數據行。表FACULTY_RECORDSLECTURE_START_TIME和學校提到了格式時隙00' 列LECTURE_TIME_SLOT提供給教師0900-1200「:需要幫助的SQL查詢

的教師填補了他/她的講座開始時間格式爲'10:15在表SCHOOL_RECORDS

現在,我有一個SQL查詢其是否通過教師對他特定的主題進入時間落在由學校或沒有分配的時隙。但是,對於SQL查詢,我得到多個輸出行,因爲相同的開始時間落在多個時間段中。然而,如果演講開始時間落在分配給同一主題的時間段內,我要求查詢僅提供一個輸出。

請參閱我的SQL查詢:

SELECT 
    a.FACULTY, 
    a.SUBJECT, 
    a.LECTURE_DATE, 
    a.LECTURE_START_TIME 
FROM 
    (SELECT 
     b.LECTURE_TIME_SLOT, b.FACULTY, b.SUBJECT, b.LECTURE_DATE, 
     CAST(LEFT(b.LECTURE_TIME_SLOT, 2) + ':' + RIGHT(LEFT(b.LECTURE_TIME_SLOT, 4), 2) AS TIME) AS StartHours, 
     CAST(LEFT(RIGHT(b.LECTURE_TIME_SLOT, 4), 2) + ':' + RIGHT(b.LECTURE_TIME_SLOT, 2) AS TIME) AS EndHours 
    FROM 
     @FACULTY_RECORDS AS a, @SCHOOL_RECORDS AS b) AS b 
JOIN 
    @FACULTY_RECORDS AS a ON a.FACULTY = b.FACULTY 
          AND a.SUBJECT = b.SUBJECT 
          AND a.LECTURE_DATE = b.LECTURE_DATE 
WHERE 
    CAST(a.LECTURE_START_TIME AS TIME) >= b.StartHours 
    AND CAST(a.LECTURE_START_TIME AS TIME) <= b.EndHours 

請參考以下圖片:

FACULTY_RECORDS & SCHOOL_RECORDS

Outputs: Lectures not matching & matching the time slots

+0

MySQL是不是SQL服務器。你在用哪個? – shmosel

+0

對不起。我正在使用Microsoft SQL Server 2008,而不是mysql。 –

+0

這對你有用嗎? https://programmingistheway.wordpress.com/2015/04/16/sql-server-loop-over-a-table/ –

回答

0

目前尚不清楚爲什麼你使用的是隱含的交叉連接(笛卡爾積)的2個表。如果有樣本數據和預期結果,這真的會有所幫助。在沒有這些的情況下,這有幫助嗎?

select 
    f.SUBJECT 
    , f.LECTURE_DATE 
    , f.LECTURE_START_TIME 
    , s.StartTime 
    , s.EndTime 
FROM FACUTLY_RECORDS f 
INNER JOIN (
     SELECT 
      SUBJECT 
      , LECTURE_DATE 
      , CAST(LEFT(LECTURE_TIME_SLOT, 2) 
       + ':' 
       + substring(LECTURE_TIME_SLOT, 3, 2) AS time) AS StartTime 
      , CAST(substring(LECTURE_TIME_SLOT, 6,2) 
       + ':' 
       + RIGHT(LECTURE_TIME_SLOT, 2) AS time) AS EndTime 
     FROM SCHOOL_RECORDS 
    ) s ON f.subject = s.subject 
      AND f.LECTURE_DATE = s.LECTURE_DATE 
      AND CAST(f.LECTURE_START_TIME AS time) BETWEEN s.StartTime AND s.EndTime 
; 

這是另一種不使用時間轉換的方法。它不會嘗試將'26:10:00'轉換爲時間。它僅使用內部聯接來列出來自兩個表的匹配。

SELECT 
     f.SUBJECT 
    , f.LECTURE_DATE 
    , f.LECTURE_START_TIME 
    , s.StartTime 
    , s.EndTime 
FROM FACUTLY_RECORDS f 
     INNER JOIN (
      SELECT 
        SUBJECT 
       , LECTURE_DATE 
       , LEFT(LECTURE_TIME_SLOT, 4) AS StartTime 
       , RIGHT(LECTURE_TIME_SLOT, 4) AS EndTime 
      FROM SCHOOL_RECORDS 
    ) s ON f.subject = s.subject 
        AND f.LECTURE_DATE = s.LECTURE_DATE 
        AND replace(left(f.LECTURE_START_TIME,5),':','') BETWEEN s.StartTime AND s.EndTime 
; 

我建議你提供更完整的測試數據,當然還有預期的結果,但仍然不存在。

queries

CREATE TABLE FACULTY_RECORDS 
    ([FACULTY_NAME] varchar(4), [SUBJECT] varchar(7), [LECTURE_DATE] datetime, [LECTURE_START_TIME] varchar(8)) 
; 

INSERT INTO FACULTY_RECORDS 
    ([FACULTY_NAME], [SUBJECT], [LECTURE_DATE], [LECTURE_START_TIME]) 
VALUES 
    ('Alex', 'Biology', '2015-01-10 00:00:00', '3:55:36'), 
    ('Alex', 'Biology', '2015-01-10 00:00:00', '10:7:22'), 
    ('Alex', 'Biology', '2015-01-10 00:00:00', '11:17:44') 
; 

CREATE TABLE SCHOOL_RECORDS 
    ([FACULTY_NAME] varchar(4), [SUBJECT] varchar(7), [LECTURE_DATE] datetime, [LECTURE_TIME_SLOT] varchar(9)) 
; 

INSERT INTO SCHOOL_RECORDS 
    ([FACULTY_NAME], [SUBJECT], [LECTURE_DATE], [LECTURE_TIME_SLOT]) 
VALUES 
    ('Alex', 'Biology', '2015-01-10 00:00:00', '0900-1200'), 
    ('Alex', 'Biology', '2015-01-10 00:00:00', '0900-1200'), 
    ('Alex', 'Biology', '2015-01-10 00:00:00', '0900-1200') 
; 
+0

嗨,這個查詢不起作用。我故意將'LECTURE_START_TIME'的值更改爲(26:10:00),並將其與'LECTURE_TIME_SLOT'值(0900-2400)進行檢查。我可以在上面的查詢輸出中看到'26:10:00'的值。理想情況下,它不應該反映在我的輸出中。我猜'BETWEEN'條款不起作用? –

+0

我又特意改變了值「LECTURE_START_TIME」作爲(三點10分00秒),它是針對「LECTURE_TIME_SLOT」值(0900-2400)進行檢查。我可以在上面的查詢輸出中看到'03:10:00'的值。理想情況下,它不應該反映在我的輸出中。對於開始時間爲'03:10:00'的行,查詢生成的StartTime和EndTime值爲'NULL' –

+0

使用innérj oin而不是左外連接。我將修改答案foo –