0

编辑:我将代码清理为以下内容,但仍然无法正常工作,即使我使用 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 来完成,但我不明白为什么它不起作用!

4

2 回答 2

2

我认为您遇到的问题是由于使用聚合函数时缺少 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`
于 2013-05-29T21:05:33.977 回答
0

你有没有尝试过...

select c.id, a.address_id, cr.practice_id
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 )
于 2013-05-23T22:41:15.997 回答