특정 열에 대한 합계를 계산하는 쿼리를 실행하고 싶습니다. 이것을 위해 내가 일치하는 레코드를 매핑하고 결과를 제공 할 두 테이블을 조인해야합니다. 아래에 내 쿼리를 붙여 넣으십시오. 내 쿼리에서 잘못된 위치를 수정하십시오. 잘못된 결과를 표시하므로 매핑이 두 번있을 수 있습니다.mysql cakephp 여러 테이블을 결합하여 열 합계를 계산하는 쿼리
$this->Inventory->find('all',array('joins'=>array(
array('table' => 'items',
'alias' => 'item',
'type' => 'left',
'conditions' => array(
'Inventory.item_id = item.id')
),
array('table' => 'material_owners',
'alias' => 'owner',
'type' => 'left',
'conditions' => array(
'Inventory.material_owner_id = owner.id')
),
array('table' => 'projects',
'alias' => 'project',
'type' => 'left',
'conditions' => array(
'Inventory.project_id = project.id')
),
array('table' => 'material_payments',
'alias' => 'mp',
'type' => 'left',
'conditions' => array(
'Inventory.material_owner_id=mp.material_owner_id'),
),
),
'conditions'=>array('Inventory.project_id'=>$project_id),
'fields' =>array('sum(Inventory.total_amount) as total_amount','sum(mp.paid_amount) as paid_amount','item.name','item.id','owner.id','owner.first_name','owner.last_name','project.id','project.name'),
'group'=> array('item.name','item.id','owner.id','owner.first_name','owner.last_name','project.name','project.id')
)
);
cakephp가 생성 한 결과 쿼리입니다.
SELECT sum("Inventory"."total_amount") as total_amount, sum("mp"."paid_amount") as paid_amount, "item"."name" AS "item__name", "item"."id" AS "item__id", "owner"."id" AS "owner__id", "owner"."first_name" AS "owner__first_name", "owner"."last_name" AS "owner__last_name", "project"."id" AS "project__id", "project"."name" AS "project__name" FROM "inventories" AS "Inventory" left JOIN "items" AS "item" ON ("Inventory"."item_id" = "item"."id") left JOIN "material_owners" AS "owner" ON ("Inventory"."material_owner_id" = "owner"."id") left JOIN "projects" AS "project" ON ("Inventory"."project_id" = "project"."id") left JOIN "material_payments" AS "mp" ON ("Inventory"."material_owner_id" = "mp"."material_owner_id") LEFT JOIN "items" AS "Item" ON ("Inventory"."item_id" = "Item"."id") LEFT JOIN "units" AS "Unit" ON ("Inventory"."unit_id" = "Unit"."id") LEFT JOIN "projects" AS "Project" ON ("Inventory"."project_id" = "Project"."id") LEFT JOIN "material_owners" AS "MaterialOwner" ON ("Inventory"."project_id" = "MaterialOwner"."id") WHERE "Inventory"."project_id" = '4' GROUP BY "item"."name", "item"."id", "owner"."id", "owner"."first_name", "owner"."last_name", "project"."name", "project"."id"
정확한 계산을 얻기 위해 하위 쿼리를 작성해야 할 수도 있습니다.하지만 케이크 PHP에서 바로 하위 쿼리를 만드는 방법을 알고 있습니다 .plpl help –
생성 된 쿼리를 붙여 넣으십시오. 도움이 될 것입니다. –