有人知道如何在Zend Framework 2中构建这样的SQL查询吗?
谢谢!
SQL:
SELECT "product"."id", "image"."id" AS "image_id"
FROM "product" AS "product"
LEFT JOIN (
"product_image"
INNER JOIN "image" ON "image"."id" = "product_image"."image_id"
) ON "product_image"."product_id" = "product"."id"
LEFT JOIN ("partner_offer"
INNER JOIN "partner" ON "partner"."id" = "partner_offer"."partner_id"
INNER JOIN "address" ON "address"."id" = "partner"."address_id"
) ON "partner_offer"."product_id" = "product"."id"
INNER JOIN (
"product_category_front"
INNER JOIN product_category_front AS "pc" ON product_category_front.lft BETWEEN pc.lft AND pc.rgt AND pc.id = 2
) ON "product_category_front"."id" = "product"."category_id"
WHERE "product"."active" = '1' LIMIT 10
答案 0 :(得分:1)
如果连接上的()之间的sql是subselects,那么这就是你想要的。
// create subselects
$selectImage = new Select('product_image');
$selectImage->join('image', 'image.id = product_image.image_id');
$selectPartner = new Select('partner_offer');
$selectPartner->join('partner', 'partner.id = partner_offer.partner_id');
$selectPartner->join('address', 'address.id = partner.address_id');
$selectCategory = new Select('product_category_front');
$where = new Where(); // this is so that the 2 doesnt get qouted wrong.
$where->expression('product_category_front.lft BETWEEN pc.lft AND pc.rgt', array())->and->equalTo("pc.id", 2);
$selectCategory->join(array('pc' => 'product_category_front'), $where);
//create main select
$select = new Select('product');
$select->columns(array('product.id', 'image_id' => 'image.id'));
$select->join(array('product_image' => $selectImage), "product_image.product_id = product.id", array(), SELECT::JOIN_LEFT);
$select->join(array('partner_offer' => $selectPartner), "partner_offer.product_id = product.id", array(), SELECT::JOIN_LEFT);
$select->join(array('product_category_front' => $selectCategory), "product_category_front.id = product.category_id", array());
$select->where->equalTo('product.active' , 1);
$select->limit(10);