gpt4 book ai didi

MYSQL 左连接不显示所有行

转载 作者:行者123 更新时间:2023-11-29 08:30:39 25 4
gpt4 key购买 nike

编辑:我将代码清理为以下内容,仍然不起作用,它仍然只显示 caregiver_review 包含匹配项 (c.id = cr.practice_id) 的行,即使我使用 LEFT JOIN

编辑2:我发现问题是由于在 SELECT 子句中使用聚合(例如 ROUND & COUNT)引起的,如果我删除 ROUND & COUNT,则所有行都会成功显示在 View 表中。我需要同时使用两者,但我仍然无法找到解决方案,有谁知道为什么它在使用 ROUND & COUNT 时改变结果以及我如何才能纠正这个问题?

查询来自名为 view_caregiver 的 View (只读)表,这些是涉及的表:
看护者
地址
caregiver_review

SELECT `c`.`id` AS `id`,`c`.`user_id` AS `user_id`,`c`.`address_id` AS `address_id`,`c`.`first_name` AS `first_name`,`c`.`last_name` AS `last_name`,`c`.`email` AS `email`,`c`.`phone` AS `phone`,`c`.`status_id` AS `status_id`,`c`.`summary` AS `summary`,`c`.`about` AS `about`,`c`.`business_name` AS `business_name`,`c`.`medical_experience` AS `medical_experience`,`c`.`traveling_distance` AS `traveling_distance`,`c`.`accepting_new_patients` AS `accepting_new_patients`,`a`.`address1` AS `address1`,`a`.`address2` AS `address2`,`a`.`city` AS `city`,`a`.`state_id` AS `address_state`,`a`.`latitude` AS `latitude`,`a`.`longitude` AS `longitude`, COUNT(DISTINCT `cr`.`id`) AS `rating_count`, ROUND((AVG((((`cr`.`rating_knowledge` + `cr`.`rating_personality`) + `cr`.`rating_office`) + `cr`.`rating_timeliness`)) / 2),0) AS `rating`
FROM caregiver AS c
LEFT JOIN address AS a ON (c.address_id = a.id)
LEFT JOIN caregiver_review AS cr ON (c.id = cr.practice_id)

这有效,但它只显示 caregiver_review 包含 practice_id 匹配的行,我希望它显示 caregiver 表中的所有行即使没有 caregiver_review 匹配。我知道这可以通过 LEFT JOIN 来完成,但我不明白为什么它不起作用!

最佳答案

我认为您遇到的问题是由于使用聚合函数时缺少 GROUP BY 造成的。如果您没有 GROUP BY 函数,那么您将不会返回所有数据。

您有两种方法可以做到这一点。您可以在子查询中聚合并按单个列进行 GROUP BY:

SELECT `c`.`id` AS `id`,`c`.`user_id` AS `user_id`,
`c`.`address_id` AS `address_id`,`c`.`first_name` AS `first_name`,
`c`.`last_name` AS `last_name`,`c`.`email` AS `email`,
`c`.`phone` AS `phone`,`c`.`status_id` AS `status_id`,
`c`.`summary` AS `summary`,`c`.`about` AS `about`,
`c`.`business_name` AS `business_name`,
`c`.`medical_experience` AS `medical_experience`,
`c`.`traveling_distance` AS `traveling_distance`,
`c`.`accepting_new_patients` AS `accepting_new_patients`,
`a`.`address1` AS `address1`,`a`.`address2` AS `address2`,
`a`.`city` AS `city`,`a`.`state_id` AS `address_state`,
`a`.`latitude` AS `latitude`,`a`.`longitude` AS `longitude`,
cr.`rating_count`,
cr.`rating`
FROM caregiver AS c
LEFT JOIN address AS a
ON (c.address_id = a.id)
LEFT JOIN
(
select cr.practice_id,
COUNT(DISTINCT `cr`.`id`) AS `rating_count`,
ROUND((AVG((((`cr`.`rating_knowledge` + `cr`.`rating_personality`) + `cr`.`rating_office`) + `cr`.`rating_timeliness`)) / 2),0) AS `rating`
from caregiver_review AS cr
GROUP BY cr.practice_id
) cr
ON (c.id = cr.practice_id)

或者您可以聚合原始查询并将 GROUP BY 应用于 SELECT 列表中未聚合的列:

SELECT `c`.`id` AS `id`,`c`.`user_id` AS `user_id`,
`c`.`address_id` AS `address_id`,`c`.`first_name` AS `first_name`,
`c`.`last_name` AS `last_name`,`c`.`email` AS `email`,
`c`.`phone` AS `phone`,`c`.`status_id` AS `status_id`,
`c`.`summary` AS `summary`,`c`.`about` AS `about`,
`c`.`business_name` AS `business_name`,
`c`.`medical_experience` AS `medical_experience`,
`c`.`traveling_distance` AS `traveling_distance`,
`c`.`accepting_new_patients` AS `accepting_new_patients`,
`a`.`address1` AS `address1`,`a`.`address2` AS `address2`,
`a`.`city` AS `city`,`a`.`state_id` AS `address_state`,
`a`.`latitude` AS `latitude`,`a`.`longitude` AS `longitude`,
COUNT(DISTINCT `cr`.`id`) AS `rating_count`,
ROUND((AVG((((`cr`.`rating_knowledge` + `cr`.`rating_personality`) + `cr`.`rating_office`) + `cr`.`rating_timeliness`)) / 2),0) AS `rating`
FROM caregiver AS c
LEFT JOIN address AS a
ON (c.address_id = a.id)
LEFT JOIN caregiver_review AS cr
ON (c.id = cr.practice_id)
GROUP BY `c`.`id`, `c`.`user_id`, `c`.`address_id`, `c`.`first_name`
, `c`.`last_name`, `c`.`email`, `c`.`phone`, `c`.`status_id`
, `c`.`summary`, `c`.`about`, `c`.`business_name`, `c`.`medical_experience`
, `c`.`traveling_distance`, `c`.`accepting_new_patients`
, `a`.`address1`, `a`.`address2`, `a`.`city`, `a`.`state_id`
, `a`.`latitude`, `a`.`longitude`

关于MYSQL 左连接不显示所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16724953/

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