Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 查询中的2个左联接计数_Mysql_Left Join - Fatal编程技术网

Mysql 查询中的2个左联接计数

Mysql 查询中的2个左联接计数,mysql,left-join,Mysql,Left Join,我有一个下载组件,希望类别显示子类别和项目数提前。两者中的1都有效,但当试图同时获得两者时,结果是两者的乘积 我使用的代码是: 选择一个*, 将(b.parentid)计数为CATScont, 将(c.id)计数为itemscount FROM(jos_foc_downl_类别作为左连接jos_foc_downl_项目作为c ON c.catid=a.id) 在b.parentid=a.id上以b左键连接jos\u foc\u downl\u类别 其中a.parentid=0 按a.id分组 这

我有一个下载组件,希望类别显示子类别和项目数提前。两者中的1都有效,但当试图同时获得两者时,结果是两者的乘积

我使用的代码是:

选择一个*,
将(b.parentid)计数为CATScont,
将(c.id)计数为itemscount
FROM(jos_foc_downl_类别作为左连接jos_foc_downl_项目作为c ON c.catid=a.id)
在b.parentid=a.id上以b左键连接jos\u foc\u downl\u类别
其中a.parentid=0
按a.id分组

这是一个类别的结果,其中CatScont为20个,ItemScont为20个,共有4个子类别和5个文件


这个怎么了?谢谢

您正在计算所有行,包括重复的值。使用DISTINCT仅对每个DISTINCT值计数一次:

SELECT a.*,
count(DISTINCT b.parentid) AS catscount,
count(DISTINCT c.id) AS itemscount
....

我必须在每次计数时按条件添加一个组,但它起了作用。谢谢