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 の user1 のすべてのパネル ( B,D,E
) と、user1=5 のパネルの合計数、および status=0 の user1 のパネルのすべての数、つまり単一のクエリで 2 が必要です。
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
これに対する別の解決策は、以下のサブクエリですが、それを使用する必要はありません
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
提案してください、ありがとう。