编辑:我将代码清理为以下内容,但仍然无法正常工作,即使我使用 LEFT JOIN,它仍然只显示 caregiver_review 包含匹配项 (c.id = cr.practice_id) 的行
编辑 2:我发现问题是由在 SELECT 子句中使用聚合引起的,例如 ROUND & COUNT,如果我删除 ROUND & COUNT,则所有行都成功显示在视图表中。我需要同时使用两者,但我仍然无法找到解决方案,有谁知道为什么在使用 ROUND & COUNT 时会改变结果以及如何解决这个问题?
查询来自名为 view_caregiver 的视图(只读)表,这些是涉及的表:
护理人员
地址
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 来完成,但我不明白为什么它不起作用!