gpt4 book ai didi

PHP MySQL : Count number of elements that are not present in another table

转载 作者:行者123 更新时间:2023-11-29 05:04:52 24 4
gpt4 key购买 nike

我现在有这种表

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
-------------------

所以基本上我需要计算表 b 中不存在但存在于表 a 中的 block 数,并根据 block 名称对其进行排序,这可能吗?因为我需要将计数结果显示为图表

最佳答案

select a.block
, 2 - count(distinct b.cid)
from a
left join b on a.pid = b.pid
group by 1

关于PHP MySQL : Count number of elements that are not present in another table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50405870/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com