[英]MySQL Query, multiple counts and sums
I have a MySQL query that outputs to a php table but I'm having issues in joining two tables that both use a COUNT: 我有一个输出到php表的MySQL查询,但是在连接两个都使用COUNT的表时遇到问题:
$query = "SELECT mqe.registration,
COUNT(*) AS numberofenqs,
COUNT(DISTINCT ucv.ip) AS unique_views,
SUM(ucv.views) AS total_views
FROM main_quick_enquiries AS mqe
LEFT OUTER JOIN used_car_views AS ucv
ON ucv.numberplate = mqe.registration
WHERE mqe.registration IS NOT NULL
GROUP BY mqe.registration ORDER BY numberofenqs DESC";
The query runs, but the number within the numberofenqs column is always wrong as i know from performing that query on its own that it comes in with the correct result: 查询运行,但是numberofenqs列中的数字始终是错误的,正如我从单独执行该查询所知道的那样,它具有正确的结果:
SELECT registration, COUNT(*) AS numberofenqs FROM main_quick_enquiries GROUP BY registration ORDER BY numberofenqs DESC
Why is the COUNT(*) not working correctly in top query code and where is it getting the figures from? 为什么COUNT(*)在顶部查询代码中无法正常工作,以及从何处获得数据?
it could be because of LEFT OUTER JOIN ... 可能是因为左外连接...
Try to run this: 尝试运行此命令:
SELECT registration
, count(*)
FROM main_quick_enquiries
GROUP BY registration
and compare it with this result 并将其与此结果进行比较
SELECT mqe.registration
, count(*)
FROM main_quick_enquiries mqe
LEFT OUTER JOIN used_car_views ucv
ON ucv.numberplate = mqe.registration
GROUP BY mqe.registration
There could be a problem :) in duplicity rows... try to find one specific registration number, and compare the details of both query 可能有问题:)在重复行中...尝试找到一个特定的注册号,并比较两个查询的详细信息
SELECT *
FROM main_quick_enquiries
WHERE registration = XXXX
+ +
SELECT *
FROM main_quick_enquiries mqe
LEFT OUTER JOIN used_car_views ucv
ON ucv.numberplate = mqe.registration
WHERE registration = XXXX
you should see the diffs 你应该看到差异
谢谢大家,但我想我用COUNT(DISTINCT mqe.id)代替了COUNT(*)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.