php mysql:计算其他表中不存在的元素数
我现在有这种桌子php mysql:计算其他表中不存在的元素数,php,mysql,mysqli,Php,Mysql,Mysqli,我现在有这种桌子 table a ============== pid | block -------------- 1 | 1 2 | 1 3 | 2 4 | 3 5 | 2 6 | 3 -------------- table b ============== cid | pid -------------- 1 | 1 2 | 3 3 | 5 -------------- output (count eac
table a
==============
pid | block
--------------
1 | 1
2 | 1
3 | 2
4 | 3
5 | 2
6 | 3
--------------
table b
==============
cid | pid
--------------
1 | 1
2 | 3
3 | 5
--------------
output (count each block who no exist on table b)
===================
block | total
-------------------
1 | 1 <--- because on table b exist 1 from 2
2 | 0 <--- because on table b exist 2 from 2
3 | 2 <--- because on table b not exist
-------------------
表a
==============
pid |块
--------------
1 | 1
2 | 1
3 | 2
4 | 3
5 | 2
6 | 3
--------------
表b
==============
cid | pid
--------------
1 | 1
2 | 3
3 | 5
--------------
输出(统计表b中不存在的每个块)
===================
区块|总计
-------------------
1 | 1您可以使用以下SQL:
select outera.block , count(outera.block)- (select count(b.pid) from a left join b on a.pid=b.pid where a.block=outera.block
group by a.block) as acount
from a as outera group by outera.block;
请让我知道它是否有效。
谢谢。为什么块id 1有计数1,你能详细解释一下吗?你可以在表a上看到块1存在2次,在pid 1和pid 2上,同时在表b上,块1只添加了1次,即pid 1sorry@holgrich,我取消了接受的答案,因为我在表a上添加另一个数据时得到的结果不正确,例如,pid 8的值为3,3的结果仍然为2,假设为3,这里是小提琴
select outera.block , count(outera.block)- (select count(b.pid) from a left join b on a.pid=b.pid where a.block=outera.block
group by a.block) as acount
from a as outera group by outera.block;