2015-08-28 151 views
1

我對使用連接和從多個表中進行選擇相當新穎。我已經成功地想出瞭如何根據許多條件從2個表中獲取數據。現在我的問題是,只有在滿足其他條件的情況下,我才需要返回計數。這個想法是這樣的: 我有一個查詢,返回在特定時間處於特定狀態的項目數量。我對這個查詢:具有多個條件和子選擇的SQL查詢

SELECT count(*), 
     e.campus_id, 
     e.course_id 
FROM statuses_history AS sh, 
    enrolments AS e 
WHERE sh.date_added > '2015-08-01 00:00:00' 
    AND sh.date_added < '2015-08-20 23:59:59' 
    AND sh.status_id = 57 
    AND sh.item_id = e.enrolment_id 
    AND (e.course_id = 2 
     OR e.course_id =7 
     OR e.course_id = 8 
     OR e.course_id = 9) 
GROUP BY e.campus_id, 
     e.course_id; 

現在我必須檢查它已經在不同的狀態,可以說sh.status_id = 50是在57之前,它也沒有落入數據中指定的範圍。所以我基本上需要以某種方式更改我的查詢,以選擇它是否曾經在status_id 50中,然後只在找到兩個狀態時才返回結果。 sh.status_id = 57將被指定的日期綁定。

在此先感謝您的幫助。

回答

1

您可以根據需要調整子查詢,但如果在數據庫中找到另一個值,則會返回一行。

我不喜歡子選擇,但這應該工作。我沒有測試過這個語法。

select count(*), e.campus_id, e.course_id 
FROM statuses_history as sh, enrolments as e 
WHERE sh.date_added > '2015-08-01 00:00:00' 
     AND sh.date_added < '2015-08-20 23:59:59' 
     AND sh.status_id = 57 
     AND sh.item_id = e.enrolment_id 
     AND (e.course_id = 2 OR e.course_id =7 
      OR e.course_id = 8 OR e.course_id = 9) 
     AND EXISTS (select sh2.status_id from statuses_history as sh2 
       WHERE NOT sh2.status_id = 57 
        AND sh2.item_id = e.enrolment_id) 
GROUP BY e.campus_id, e.course_id; 
+0

很好的答案。儘管你在「AND EXISTS(從statuses_history中選擇sh2.status_id」)中犯了一個錯誤,但我只是添加了「as sh2」,但除此之外它完美地工作,謝謝。 –

1

我假定主鍵是item_id?

您只需在主鍵和STATUS_ID = 50

對於這種情況,我將使用INNER JOIN(我的偏好)加入到同一個表,但你可以使用FROM/WHERE子句。

select count(*), e.campus_id, e.course_id 
FROM statuses_history as sh, enrolments as e 

INNER JOIN statuses_history AS oldSh ON oldSh.item_id = sh.item_id AND oldSh.status_id = 50 

WHERE sh.date_added > '2015-08-01 00:00:00' AND sh.date_added < '2015-08-20 23:59:59' AND sh.status_id = 57 and sh.item_id = e.enrolment_id AND (e.course_id = 2 OR e.course_id =7 OR e.course_id = 8 OR e.course_id = 9) 
GROUP BY e.campus_id, e.course_id; 

但是,如果你更喜歡哪裏,你可以做到以下幾點:

select count(*), e.campus_id, e.course_id 
FROM statuses_history as sh, enrolments as e, statuses_history AS oldSh ON 
WHERE oldSh.item_id = sh.item_id AND oldSh.status_id = 50 AND 
sh.date_added > '2015-08-01 00:00:00' AND sh.date_added < '2015-08-20 23:59:59' AND sh.status_id = 57 and sh.item_id = e.enrolment_id AND (e.course_id = 2 OR e.course_id =7 OR e.course_id = 8 OR e.course_id = 9) 
GROUP BY e.campus_id, e.course_id; 
1

添加一個左連接,並使用一個case語句做你的計數。

LEFT JOIN statuses_history sh2 ON sh2.item_id = e.enrollment_id AND sh2.status_id = 50 

您需要爲內連接使用不同的格式才能進行左連接。這裏是新的查詢:

select count(sh.status_id), e.campus_id, e.course_id , SUM(CASE WHEN sh2.status_id IS NOT NULL THEN 1 ELSE 0 END) as cnt 
FROM statuses_history sh 
INNER JOIN enrolments e ON sh.item_id = e.enrolment_id 
LEFT JOIN statuses_history sh2 ON sh2.item_id = e.enrollment_id AND sh2.status_id = 50 
WHERE sh.date_added > '2015-08-01 00:00:00' AND sh.date_added < '2015-08-20 23:59:59' AND sh.status_id = 57 AND (e.course_id = 2 OR e.course_id =7 OR e.course_id = 8 OR e.course_id = 9) 
GROUP BY e.campus_id, e.course_id;