gpt4 book ai didi

MySQL 查询、多次计数和求和

转载 作者:行者123 更新时间:2023-11-29 12:33:14 24 4
gpt4 key购买 nike

我有一个输出到 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";

查询运行,但 numberofenqs 列中的数字总是错误的,因为我通过单独执行该查询知道它会带来正确的结果:

SELECT registration, COUNT(*) AS numberofenqs FROM main_quick_enquiries GROUP BY registration ORDER BY numberofenqs DESC

为什么 COUNT(*) 在顶级查询代码中无法正常工作?它从哪里获取数字?

最佳答案

这可能是因为 LEFT OUTER JOIN ...

尝试运行这个:

SELECT registration
, count(*)
FROM main_quick_enquiries
GROUP BY registration

并将其与此结果进行比较

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

口是心非的行中可能存在问题...尝试查找一个特定的注册号,并比较两个查询的详细信息

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

你应该看到差异

关于MySQL 查询、多次计数和求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27171500/

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