table:panel
id PanelName email_id status
1 A 1 0
2 B 1 1
3 C 1 0
4 D 1 1
5 E 1 1
-------------------------
6 A1 2 0
7 B1 2 1
8 C1 2 0
-------------------------
9 D1 3 1
10 E1 3 1
我需要與狀態1(B,D,E
) USER1的所有面板與總無USER1 = 5的面板和與用戶1的面板的全部計數的沿狀態= 0,即2在一個單一的查詢。將所有記錄與具有不同狀態記錄計數一起在MySQL
select p.*,Count(p1.id) as totalPanels
from panel p
INNER join panel p1 on (p.email_id=p1.email_id)
where p.email_id=1 and p.status =1
Group by p.id
這給我所有面板狀態1和總面板數。但如何在同一個查詢中帶有狀態爲0的面板數?
Expected output will be:
id |PanelName| email_id |status| TotalPanel| Rejectedpanel
1 A 1 0 5 2
2 B 1 1 5 2
3 C 1 0 5 2
4 D 1 1 5 2
5 E 1 1 5 2
另一個soution這是子查詢如下但是我沒有使用該
select p.id,Count(p1.id) as totalPanels,
(select count(id) from panel p2 where p2.status=0 and p2.email_id=p.email_id) as RejectePanel
from panel p
INNER join panel p1 on (p.email_id=p1.email_id)
where p.email_id=1
Group by p.id
請建議,謝謝。
嘗試在(0,1)' –
這個'p.status但我需要的東西是這樣的:選擇P *,COUNT(p1.id)作爲totalPanels,Count(p1.id)作爲RejectedPanels ,, – Gerry
@Gerry你問很多東西,你可以把它們放在列表中,使其更容易理解。 –