Doctrine 的累积 DQL [英] Cumulative DQL with Doctrine

查看:30
本文介绍了Doctrine 的累积 DQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难制定出合适的 DQL 来生成累积总和.我可以用普通的 SQL 来做,但是当涉及到 DQL 时,我无法掌握它.

Im having a hard time working out a proper DQL to generate cumulative sum. I can do it in plain SQL but when it comes to DQL i cant get hold of it.

这是它在 SQL 中的样子:

Here is how it looks in SQL:

<代码>

    SELECT s.name, p.date_short, p.nettobuy,
    (select sum(pp.nettobuy) as sum from price pp where pp.stock_id = p.stock_id and p.broker_id = pp.broker_id and pp.date_short <= p.date_short) as cumulative_sum
FROM price p
    left join stock s on p.stock_id = s.id
    group by p.stock_id, p.date_short
    order by p.stock_id, p.date_short

谢谢

推荐答案

你好,我查看了 Doctrine 1.2 的文档,创建查询的方法是(注意别名):

Hey, I have check the documentation for Doctrine 1.2, and the way to create the query is (put attention on the alias):

$query = Doctrine_Query::create();
$query->addSelect('AVG(price) as price');
$query->addSelect('AVG(cost) as cost');
// as many addSelect() as you need
$query->from('my_table');

输出创建的 SQL 查询:

To output the SQL query created:

echo $query->getSqlQuery();

执行语句:

$product = $query->fetchOne();

访问检索到的数据是:

echo $product->getPrice();
echo $product->getCost();

阅读 按子句分组.

这篇关于Doctrine 的累积 DQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆