Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.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查询返回错误的计数值_Mysql - Fatal编程技术网

MySQL查询返回错误的计数值

MySQL查询返回错误的计数值,mysql,Mysql,这里我分享了我的疑问。所有的总值都给出了错误的结果,即非常大的数值,而不是期望的计数值。我的问题出了什么问题 SELECT bd.business_id, bd.business_name, bd.business_address, bd.created_at, bd.updated_at AS extent_expiry_date

这里我分享了我的疑问。所有的总值都给出了错误的结果,即非常大的数值,而不是期望的计数值。我的问题出了什么问题

           SELECT bd.business_id,
                  bd.business_name,
                  bd.business_address,
                  bd.created_at,
                  bd.updated_at AS extent_expiry_date,
                  bd.manager_id as user_id,
                  COUNT(m.manager_id) AS total_managers,
                  COUNT(a.agent_id) AS total_agents,
                  COUNT(c.customer_id) AS total_customers,
                  COUNT(t.task_id) AS total_tasks,
                  COUNT(t.order_id) AS total_orders
            FROM business_details bd
            LEFT JOIN managers m ON m.business_id = bd.business_id
            LEFT JOIN agents a ON a.business_id = bd.business_id
            LEFT JOIN customers c ON c.business_id = bd.business_id
            LEFT JOIN tasks t ON t.business_id = bd.business_id
            GROUP BY bd.business_id

由于您的问题中没有样本数据集,我猜您得到的是相同的经理id、代理id。。。多次由于左连接而获得正确的计数使用distinct

SELECT bd.business_id,
      bd.business_name,
      bd.business_address,
      bd.created_at,
      bd.updated_at AS extent_expiry_date,
      bd.manager_id as user_id,
      COUNT(distinct m.manager_id) AS total_managers,
      COUNT(distinct a.agent_id) AS total_agents,
      COUNT(distinct c.customer_id) AS total_customers,
      COUNT(distinct t.task_id) AS total_tasks,
      COUNT(distinct t.order_id) AS total_orders
FROM business_details bd
LEFT JOIN managers m ON m.business_id = bd.business_id
LEFT JOIN agents a ON a.business_id = bd.business_id
LEFT JOIN customers c ON c.business_id = bd.business_id
LEFT JOIN tasks t ON t.business_id = bd.business_id
GROUP BY bd.business_id
根据新版本的规定,还应将所有非聚合列包含在group by中,并且大多数RDBMS拒绝此类查询

GROUP BY 
bd.business_id,
bd.business_name,
bd.business_address,
bd.created_at,
bd.updated_at,
bd.manager_id

由于您的问题中没有样本数据集,我猜您得到的是相同的经理id、代理id。。。多次由于左连接而获得正确的计数使用distinct

SELECT bd.business_id,
      bd.business_name,
      bd.business_address,
      bd.created_at,
      bd.updated_at AS extent_expiry_date,
      bd.manager_id as user_id,
      COUNT(distinct m.manager_id) AS total_managers,
      COUNT(distinct a.agent_id) AS total_agents,
      COUNT(distinct c.customer_id) AS total_customers,
      COUNT(distinct t.task_id) AS total_tasks,
      COUNT(distinct t.order_id) AS total_orders
FROM business_details bd
LEFT JOIN managers m ON m.business_id = bd.business_id
LEFT JOIN agents a ON a.business_id = bd.business_id
LEFT JOIN customers c ON c.business_id = bd.business_id
LEFT JOIN tasks t ON t.business_id = bd.business_id
GROUP BY bd.business_id
根据新版本的规定,还应将所有非聚合列包含在group by中,并且大多数RDBMS拒绝此类查询

GROUP BY 
bd.business_id,
bd.business_name,
bd.business_address,
bd.created_at,
bd.updated_at,
bd.manager_id

我假设您的所有表在
business\u id
上都不是唯一的?如果没有,那么如果一个企业有3个经理和3个代理人,你会得到9行。。。您是否查看了单个业务的原始数据以确定是否选择了所需的数据?我假设您的所有表在
业务id
上都不是唯一的?如果没有,那么如果一个企业有3个经理和3个代理人,你会得到9行。。。您是否查看了单个业务的原始数据,以确定是否选择了所需的数据?