mysql cakephp查询连接多个表以获取列的总和

时间:2012-12-21 11:15:56

标签: 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"

1 个答案:

答案 0 :(得分:0)

我不确定你在这里要做什么。你的蛋糕查询是否有效? 您获得了哪些错误或者您希望输出如何?

对于溶剂,我会看看虚拟场。 http://book.cakephp.org/2.0/en/models/virtual-fields.html

在使用连接进行查询之前,可以将它们添加到每个模型中。