双LEFT JOIN

时间:2011-12-07 14:30:17

标签: sql zend-framework left-join

我想收到属性名称和单位数以及指定数量。我有这个问题:

SELECT 
  `property`.`property_name`,
  COUNT(unit_id) AS `units_count`,
  COUNT(special_id) AS `specials_count` 
FROM `property`
  LEFT JOIN `property_unit` ON unit_property_id = property_id
  LEFT JOIN `property_special` ON special_property_id = property_id
WHERE (property_id = '1')
GROUP BY `property_id`
ORDER BY `property_name` ASC

但它无法正常工作。如果我有其中一个左连接 - 没关系,但如果我有两个,我得到这个结果:

["property_name"] => string(11) "Rivers Edge"
["units_count"] => string(1) "2"
["specials_count"] => string(1) "2"

特价计数为2,units_count为2,但单位计数实际为'1'。我怎样才能得到正确的计数呢?

P.S:对于那些了解Zend Framework的人:

$select->setIntegrityCheck(FALSE)
    ->from(
        'property',
        array(
            'property_name',
        )
    )
    ->joinLeft(
        'property_unit',
        'unit_property_id = property_id',
        array(
            'units_count' => 'COUNT(unit_id)'
        )
    )
    ->joinLeft(
        'property_special',
        'special_property_id = property_id',
        array(
            'specials_count' => 'COUNT(special_id)'
        )
    )
    ->group('property_id')
    ->order('property_name');

2 个答案:

答案 0 :(得分:11)

试试这个:

SELECT 
  `property`.`property_name`,
  COUNT(distinct unit_id) AS `units_count`,
  COUNT(distinct special_id) AS `specials_count` 
FROM `property`
  LEFT JOIN `property_unit` ON unit_property_id = property_id
  LEFT JOIN `property_special` ON special_property_id = property_id
WHERE (property_id = '1')
GROUP BY `property_id`
ORDER BY `property_name` ASC

编辑:

你不应该总是使用distinct - 在这种情况下它恰好是正确的选项。

select count(fieldname)返回fieldname不为null的次数; select count(distinct fieldname)返回fieldname的不同值的数量。

在原始查询中,property_unit和property_special不会相互连接,只会连接到属性 - 因此对于具有5个单位和7个特殊内容的单个属性,将返回35行;因此count(unit_id)count(special_id)都会返回35.因为unit_id有5个不同的值,而special_id有7个不同的值(因为这些字段唯一地标识了他们的记录),{ {1}}会在这些情况下返回正确的值。

答案 1 :(得分:1)

你的SQL应该是这样的:

SELECT 
  `property`.`property_name`,
  COUNT(property_unit.unit_id) AS `units_count`,
  COUNT(property_special.special_id) AS `specials_count` 
FROM `property`
  LEFT JOIN `property_unit` ON (property_unit.unit_property_id = property.property_id)
  LEFT JOIN `property_special` ON (property_special.special_property_id = property.property_id)
WHERE (property.property_id = '1')
GROUP BY `property.property_id`
ORDER BY `property.property_name` ASC