为了账号安全,请及时绑定邮箱和手机立即绑定

学说区别计数

学说区别计数

PHP
泛舟湖上清波郎朗 2023-05-26 17:35:00
检索实体集合的最佳做法是什么DISTINCT COUNT?在此示例实体 ( Customer) 中,我oneToMany与 有关系Orders。我想计算客户订购了多少销售额和产品:> select * from orders;+----------+----------+----------+| customer | sale_ref | prod_ref |+----------+----------+----------+| 1        | sale_1   | prod_1   || 1        | sale_1   | prod_2   || 1        | sale_2   | prod_1   || 1        | sale_3   | prod_3   |+----------+----------+----------+> select count(prod_ref) from order where customer = 1;+-----------------+| count(prod_ref) |+-----------------+| 4               |+-----------------+> select count(distinct(sale_ref)) from order where customer = 1;+-----------------+| count(prod_ref) |+-----------------+| 3               |+-----------------+这是代码use Doctrine\ORM\Mapping as ORM;class Customer{    /**     * @var \Doctrine\Common\Collections\Collection     * @ORM\OneToMany(targetEntity="Orders", mappedBy="customer", cascade={"persist", "remove"}, fetch="EXTRA_LAZY")     */    protected $orders;    /**     * @return \Doctrine\Common\Collections\Collection     */    public function getOrders(): \Doctrine\Common\Collections\Collection    {        return $this->orders;    }    /**     * @return int     */    public function getOrdersProductCount(): int    {        return $this->orders->count();    }}class Orders{    /**     * @var Customer $customer     * @ORM\ManyToOne(targetEntity="Customer", inversedBy="orders")     */    protected $customer;    /**     * Non-unique sales reference     * @var string $salesRef     * @ORM\Column(name="sales_ref", type="string")     */    protected $salesRef;使用该Customer->getOrdersProductCount()作品非常适合检索产品数量,据说是“良好做法”,因为它不会在完全加载集合时访问数据库:https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/tutorials/extra-lazy-associations.html如果您将关联标记为额外惰性,则可以在不触发集合的完整加载的情况下调用集合的以下方法Collection#count()但是,在此示例中,aCustomer可以有多个产品用于销售 - 其中salesRef是非唯一的。DISTINCT COUNT检索的最佳方法是什么salesRef?
查看完整描述

1 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

我认为这应该做到,并且是一种更便携的方式。我没有测试它。


    $qb = $this->createQueryBuilder('o');

    return (int)$qb

        ->select($qb->expr()->countDistinct('o.salesRef'))

        ->where('o.customer = :customer')

        ->setParameter('o.customer', $customer)

        ->setMaxResults(1)

        ->getQuery()

        ->getSingleScalarResult();

查看完整回答
反对 回复 2023-05-26
  • 1 回答
  • 0 关注
  • 86 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信