Propel:需要以下 SQL 查询的条件

发布于 2024-11-19 04:49:53 字数 819 浏览 2 评论 0原文

经过半天的头痛试图找出双重 INNER JOIN 后,我终于猜对了:

SELECT product.id as product_id, count(purchase_vaucher.object_id) as purchased 
FROM purchase_vaucher 
INNER JOIN purchase on purchase.id = purchase_vaucher.object_id 
INNER JOIN product on purchase.product_id = product.id 
WHERE purchase.is_paid=1 
GROUP BY product.id 
ORDER BY purchased_amount desc;

这是关于什么的:基本上,三个表:productpurchase、< code>purchase_vaucher (是的,我知道它是“优惠券”,但我没有发明该模式)。基本上,purchase 包含所进行的购买以及指向产品 的反向链接。 purchase_vaucher 包含有关优惠券的信息和购买的反向链接。我在此查询中需要的内容:对于拥有 is_paid=1 的所有 purchase,计算具有相同产品 ID 的所有购买的所有优惠券,并按产品 ID 将它们分组。

我需要的是:将上述查询翻译为 Propel Criteria API 调用。

我马上就开始做这件事,但如果我能节省一些时间,我会很高兴。

谢谢。

After half a day of headaches trying to figure out a double INNER JOIN, I finally got it right:

SELECT product.id as product_id, count(purchase_vaucher.object_id) as purchased 
FROM purchase_vaucher 
INNER JOIN purchase on purchase.id = purchase_vaucher.object_id 
INNER JOIN product on purchase.product_id = product.id 
WHERE purchase.is_paid=1 
GROUP BY product.id 
ORDER BY purchased_amount desc;

What is this about: basically, three tables: product, purchase, purchase_vaucher (yeah I know it's "voucher", but I didn't invent the schema). Basically, purchase contains purchases made and a back-link to a product. purchase_vaucher contains info about vouchers and a back-link to a purchase. What I needed from this query: For all purchases who have is_paid=1, count all vouchers of all the purchases with the same product id and group them by the product id.

What I need is: the above query translated as a Propel Criteria API calls.

I am starting to work on this right away, but I will be happy if I could save some time.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你穿错了嫁妆 2024-11-26 04:49:53

奇怪的是,事实证明这非常简单:

$purchased_amount_column = 'purchased_amount';

$c = new Criteria();
self::addSelectColumns($c);
$c->addAsColumn($purchased_amount_column, 'COUNT(' . PurchaseVaucherPeer::OBJECT_ID . ')');
$c->addGroupByColumn(ProductPeer::ID);
$c->addDescendingOrderByColumn($purchased_amount_column);
$c->add(PurchasePeer::IS_PAID, 1, Criteria::EQUAL);
$c->addJoin(PurchasePeer::ID, PurchaseVaucherPeer::OBJECT_ID, Criteria::INNER_JOIN);
$c->addJoin(PurchasePeer::PRODUCT_ID, ProductPeer::ID, Criteria::INNER_JOIN);

寻呼机出了问题,但我花了 5 分钟才意识到让它计算如此重要的 Criteria 记录的方法很简单:

$pager->setPeerCountMethod('customCount');
...
// customCount:
return YourPeer::doCount($yourCriteria);

就是这样。

Curiously enough, it turned out to be very easy:

$purchased_amount_column = 'purchased_amount';

$c = new Criteria();
self::addSelectColumns($c);
$c->addAsColumn($purchased_amount_column, 'COUNT(' . PurchaseVaucherPeer::OBJECT_ID . ')');
$c->addGroupByColumn(ProductPeer::ID);
$c->addDescendingOrderByColumn($purchased_amount_column);
$c->add(PurchasePeer::IS_PAID, 1, Criteria::EQUAL);
$c->addJoin(PurchasePeer::ID, PurchaseVaucherPeer::OBJECT_ID, Criteria::INNER_JOIN);
$c->addJoin(PurchasePeer::PRODUCT_ID, ProductPeer::ID, Criteria::INNER_JOIN);

Had a trouble with the pager, but it took me 5 mins to realize that the way to make it count the records of such non-trivial Criteria was simply this:

$pager->setPeerCountMethod('customCount');
...
// customCount:
return YourPeer::doCount($yourCriteria);

That was it really.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文