我有多个选择字段的奇怪问题。 这是我的问题:
$query = $queryBuilder
->select(
[
'SUM(t1.quantity) AS quantity_t1',
't2.currency AS currency_t2',
'SUM(t1.amount - t1.promotionDiscount) AS amount_t1',
'SUM(ROUND((t1.amount - t1.promotionDiscount) / ((100 + t2.tax) / 100), 2)) AS amount_net',
'SUM(ROUND((t1.amount - t1.promotionDiscount) / '.EXCHANGE_RATE_GBP.', 2)) AS amount_gbp',
'SUM(ROUND(ROUND((t1.amount - t1.promotionDiscount) / '.EXCHANGE_RATE_GBP.', 2) / ((100 + t2.tax) / 100), 2)) AS amount_gbp_net'
]
)
->from(Sale::class, 't2')
->join(
SaleRow::class,
't1',
Join::WITH,
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq('t1.saleId', 't2.id')
)
)
->where(
$queryBuilder->expr()->between('t2.purchaseTime', ':start', ':end')
)
->andWhere(
$queryBuilder->expr()->in('t2.orderStatus', ':status')
)
->setParameters(
[
':start' => $startDate->format('Y-m-d H:i:s'),
':end' => $endDate->format('Y-m-d H:i:s'),
':status' => ['pending', 'shipped']
]
)
->getQuery();
$results = $query->getArrayResult();
查询有效,但仅限于我改变我的选择数组:
例如:
->select(
[
't1.id',
't2.id'
]
)
非常奇怪...... 这有效:
->select(
[
'SUM(t1.quantity) AS quantity_t1',
'SUM(t1.amount - t1.promotionDiscount) AS amount_t1'
]
)
这也是:
->select(
[
't2.currency AS currency_t2',
]
)
但是......这不起作用:
->select(
[
'SUM(t1.quantity) AS quantity_t1',
't2.currency AS currency_t2',
]
)
为什么?
答案 0 :(得分:1)
您不能在SQL查询中将聚合与非聚合混合在一起。
因为聚合将返回一个值。 非聚合将返回每个值。
如何在响应中混合这些结果?
其他问题,您在DQL中使用ROUND()
函数。但是这个功能不存在,你必须register it a custom DQL function。
请参阅this question。