我想收到屬性名稱和單位數量和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是 –