MYSQL左连接不显示所有行

时间:2013-05-23 22:30:41

标签: mysql left-join

编辑:我将代码清理到以下内容,仍然没有工作它仍然只显示 caregiver_review 包含匹配(c.id = cr.practice_id)的行,即使我使用LEFT JOIN < / p>

编辑2:我发现问题是由SELECT子句中的聚合引起的,例如ROUND&amp; COUNT,如果我删除ROUND&amp;在视图表中成功显示所有行的COUNT。我需要使用两者,我仍然无法找到解决方案,有谁知道为什么它在使用ROUND&amp;时改变结果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_review 匹配。我知道这可以通过LEFT JOIN完成,但我不明白为什么它不起作用!

2 个答案:

答案 0 :(得分: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`

答案 1 :(得分: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 )