dongxiao3694 2015-01-05 16:44
浏览 29

如何在以下查询中使用DoctrineDBAL正确准备参数?

I need to get the percentage of each possible values in the field column, over the total value of my table.

I found two way to get my result in SQL:

SELECT m.field, sum(m.value) * 100 / t.total
FROM my_table AS m 
    CROSS JOIN (
        SELECT SUM(value) AS total FROM 
        WHERE year = 2000) t
WHERE m.year = 2000
GROUP BY m.field, t.total

And

SELECT m.field, sum(m.value) * 100 / (SELECT SUM(value) AS total FROM WHERE year = 2000)
FROM my_table AS m 
WHERE m.year = 2000
GROUP BY m.field

But both are nested queries, and I don't know how to prepare statments with the Doctrine's QueryBuilder into a nested queries.

Is there a way to do it?

  • 写回答

2条回答 默认 最新

  • doufang1954 2015-01-05 19:33
    关注

    I have been trying to do so using querybuilder and DQL with no success. As it seems, DQL doesn't allow operations with subqueries in SELECT. What I've achieved so far:

    $subQuery = $em->createQueryBuilder('m')
                    ->select("SUM(m.value)")
                    ->where("m.year = 2000")
                    ->getDQL();
    

    The following query works though doesn't calculate the percentage:

    $query = $em->createQueryBuilder('f')
                    ->select("f.field")
                    ->addSelect(sprintf('(%s) AS total', $subQuery))
                    ->addSelect('(SUM(f.value)*100) AS percentage')
                    ->where("f.year = 2000")
                    ->groupBy("f.field")
                    ->getQuery()
                    ->getResult();
    

    However, if you try to add the division in the select in order to get the percentage and you use the subquery, it simply doesn't work. Looks like the construction it's not allowed in DQL. I've tried with an alias and with the subquery directly and neither of them worked.

    Doesn't work:

    $query = $em->createQueryBuilder('f')
                        ->select("f.field")
                        ->addSelect(sprintf('(%s) AS total', $subQuery))
                        ->addSelect('(SUM(f.value)*100)/total AS percentage')
                        ->where("f.year = 2000")
                        ->groupBy("f.field")
                        ->getQuery()
                        ->getResult();
    

    Doesn't work either:

    $query = $em->createQueryBuilder('f')
                        ->select("f.field")
                        ->addSelect(sprintf('(SUM(f.value)*100)/(%s) AS percentage', $subQuery))
                        ->where("f.year = 2000")
                        ->groupBy("f.field")
                        ->getQuery()
                        ->getResult();
    

    I'd suggest using SQL directly (Doctrine allows it). Using native sql queries and mapping the results would do the trick. There is no disadvantage in doing so.

    Documentation

    If you find a way of doing it using queryBuilder or DQL, please let me know.

    Hope it helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值