[英]Doctrine Query Join sub Query Syntax
我一直在嘗試將以下原始sql傳輸到dql中,但四個小時都無法執行,您能幫我嗎? 該查詢在數據庫中找到重復的哈希並給我訂單ID
SELECT h.order_id
FROM `Hash` h
INNER JOIN (SELECT `order_id`,hash
FROM `Hash`
GROUP BY `hash`
HAVING COUNT(*) > 1
) dt ON h.hash=dt.hash ;
現在我的dql:
SELECT h FROM PaymentBundle:Hash h
JOIN
(SELECT h1.order, h1.hash FROM PaymentBundle:Hash h1 GROUP BY h1.hash HAVING COUNT(h1) > 1)
dt WITH h.hash = dt.hash
但是h1.order
提供語法錯誤,以及其他所有操作:
[Semantical Error] line 0, col 40 near '(SELECT h1.hash': Error: Class '(' is not defined.
我嘗試將其作為子查詢:
$subDql = 'SELECT h1 FROM PaymentBundle:Hash h1 GROUP BY h1.hash HAVING COUNT(h1) > 1';
$subQuery = $this->getEntityManager()
->createQuery($subDql);
$dql = 'SELECT h FROM PaymentBundle:Hash h JOIN ('.$subQuery->getDQL().') dt ON h.hash = dt.hash';
$query = $this->getEntityManager()
->createQuery($dql);
return $query->getResult();
它給了我:
[學說\\ ORM \\查詢\\ QueryException]
[語義錯誤]第0行,行40位於'(SELECT h1 FROM'附近:錯誤:未定義類'('。[學說\\ ORM \\查詢\\ QueryException]
從PaymentBundle:Hash中選擇h加入(從PaymentBundle:Hash中選擇h1按h1.hash分組COUNT(h1)> 1)dt ON h.hash = dt.hash
我的解決方案:
$rsm = new ResultSetMappingBuilder($this->getEntityManager());
$rsm->addRootEntityFromClassMetadata('PaymentBundle:Hash', 'h');
$qb = $this->getEntityManager()->createNativeQuery("
SELECT h.id, h.order_id
FROM `Hash` as h
INNER JOIN (SELECT `order_id`,hash
FROM `Hash`
GROUP BY `hash`
HAVING COUNT(*) > 1
) dt ON h.hash = dt.hash
", $rsm);
return $qb->getResult();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.