2014-10-01 207 views
0

具體的計數值我有這個疑問獲取具有相同的ID列

SELECT * 
FROM classroom 
WHERE capacity > (SELECT COUNT(class_history.*) 
        FROM classroom 
        RIGHT JOIN class_history ON classroom.id_class = class_history.id_class 
        GROUP BY class_history.id_class) 
ORDER BY id_class DESC 

我想從計數class_history記錄課堂容量,同樣與課堂ID,任何人都可以幫我這個查詢?

這樣我就可以得到這樣

classroom 1 - capacity (0/20) 
classroom 2 - capacity (2/20) 
classroom 3 - capacity (9/10) 

下面的截圖返回

教室 classroom

class_history class_history

注意:如果容量已滿,然後它沒有出現,如果不是完全那麼它將如果它仍然0

+0

更詳細的表結構可能出現不管? – 2014-10-01 06:01:23

+0

此外,一些樣本數據以及您想要從中產生的結果會很好。幫助我們幫助你。 – Mureinik 2014-10-01 06:02:49

+0

感謝您的回覆,在這裏我已經把截圖 – 2014-10-01 06:07:41

回答

1

使用LEFT JOIN,它應該解決您的問題

SELECT classroom.id_class, COUNT(class_history.id_class), capacity 
FROM classroom LEFT JOIN class_history 
ON classroom.id_class = class_history.id_class 
GROUP BY classroom.id_class 
HAVING COUNT(class_history.id_class) < capacity 
+0

@Khrisna,請讓知道它是否沒有幫助? – 2014-10-01 06:25:40

+0

它的工作原理感謝..但即時通訊對不起,我忘了說,如果容量已滿,那麼它不會出現,當容量仍然爲0時,它會出現。所以當它充分它沒有出現,如果沒有充分,它會出現 – 2014-10-01 06:35:47

+0

請現在檢查.. – 2014-10-01 06:46:03