Tôi muốn nhận được tên và số lượng đơn vị và số lượng đếm. Tôi có truy vấn này:Double 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
Nhưng nó không hoạt động bình thường. Nếu tôi có một trong những trái này tham gia - đó là ok, nhưng nếu tôi có hai, tôi nhận được kết quả này:
["property_name"] => string(11) "Rivers Edge"
["units_count"] => string(1) "2"
["specials_count"] => string(1) "2"
Đặc biệt số là 2 và units_count là 2, nhưng các đơn vị đếm thực sự '1' là. Làm thế nào tôi có thể nhận được số lượng chính xác cho nó?
PS: đối với những người biết 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');
Các khóa đơn vị có phải là unit_id và special_id trên bảng property_unit và property_special không? –
@Mark Bannister Có –