0
我在Symfony 2中查詢,我試圖從聯合表中返回唯一值以及這些唯一值的總和。查詢並返回不同的值和數量總和
例如:
Inventory Table:
id | product_id | qty | warehouse_id
----------------------------------------
1 | 123 | 5 | 1
2 | 123 | 5 | 1
3 | 123 | 5 | 1
4 | 234 | 5 | 1
5 | 345 | 5 | 1
6 | 345 | 5 | 1
7 | 345 | 5 | 1
8 | 345 | 5 | 1
9 | 345 | 5 | 1
10 | 345 | 5 | 1
Product Table:
id | name | sku | description |
------------------------------------------------
123 | Test 123 | Test 123 | Test 123
234 | Test 234 | Test 234 | Test 234
345 | Test 345 | Test 345 | Test 345
到底
那麼,我需要在一個陣列是相當多以下信息加什麼都被回來從加入:
product_id: 123 qty: 15
product_id: 234 qty: 5
product_id: 345 qty: 30
這裏我的完整查詢......我不太清楚如何在Symfony 2中使用該原則來做到這一點。謝謝你的幫助!
$search_string = trim($search_string);
$em = $this->getEntityManager();
$query = $em->createQuery("
SELECT i, p, il, w
FROM WIC\InventoryBundle\Entity\Inventory i
LEFT JOIN i.product p
LEFT JOIN i.inventoryLocation il
LEFT JOIN il.warehouse w
WHERE (p.name LIKE :tag OR p.sku LIKE :tag OR p.description LIKE :tag)
AND p.account = :account_id
AND il.warehouse = :location_id");
$query->setParameters(array(
'tag' => "%$search_string%",
'account_id' => $account_id,
'location_id' => $location_id
));
return $query->getArrayResult();