我想收到属性名称和单位数量和specails数量。我有这个查询:双左连接
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”。我如何得到正确的计数呢?
PS:对于那些谁知道Zend框架:
$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');
property_unit和property_special表上的unit_id和special_id唯一键? –
@Mark Bannister是 –