gpt4 book ai didi

php - DQL复杂查询

转载 作者:可可西里 更新时间:2023-10-31 22:42:57 25 4
gpt4 key购买 nike

好的我有2个表,相关结构如下:

**youtubevideo**
id - int
allViews - relational


**views**
id - int
youtubevideo_id - int
viewCount - int
firstFetch - date

与 youtubevideo 有很多观看次数有关。这是由 Symfony 实体中的 OneToMany 呈现的。

现在,我有 2 个日期,我们称它们为 fromDate 和 toDate。我需要选择所有 youtube 视频并按属于它们的 View 的 viewCount 总和对它们进行排序。

我只想使用 firstFetch 日期介于 fromDate 和 toDate 之间的 View 。

所以我想它会是这样的

SELECT y FROM YTScraperBundle:YouTubeVideo y 
JOIN y.allViews v GROUP BY y.id
ORDER BY SUM(v.viewCount) LIMIT 0, 30; <-- or does this need to be an inline select to the specific element?

而且我真的不知道如何把 WHERE v.firstFetch is between fromDate 和 toDate 放在那里。

更新

$query = $this->em->createQuery(
"SELECT y, IF(v IS NULL, 0, SUM(v.viewCount)) AS HIDDEN sumviewcount
FROM YTScraperBundle:YouTubeVideo y
LEFT JOIN y.allViews v WITH v.firstFetch BETWEEN :fromDate AND :toDate
GROUP BY y ORDER BY sumviewcount DESC
"
);
$query->setMaxResults(self::PR_PAGE);
$query->setFirstResult($firstResult);
$query->setParameter('fromDate', $fromDate);
$query->setParameter('toDate', $toDate);

获取错误:

Expected known function, got 'IF'

最佳答案

您必须使用 WITHBETWEEN,并将两个 DateTime 对象绑定(bind)到参数化查询中:

$result = $this->getDoctrine()->getManager()->createQuery('
SELECT y,
CASE WHEN (v IS NULL) THEN 0 ELSE SUM(v.viewCount) END AS HIDDEN sumviewcount
FROM YTScraperBundle:YouTubeVideo y
LEFT JOIN y.allViews v WITH v.firstFetch BETWEEN :fromDate AND :toDate
GROUP BY y ORDER BY sumviewcount DESC
')->setParameter('fromDate', new \DateTime('12-34-5678 00:00:00')) // Replace this with an ISO date
->setParameter('toDate', new \DateTime('12-34-5678 00:00:00')) // Replace here too
->getResult();

我已经 combined @Matteo's answer concerning the SUM into mine for posterity . HIDDEN 创意归功于他。

关于php - DQL复杂查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26845489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com