We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Is there way to set up the alias name for the "AVG" column in the QueryBuilder?

Hello.

Is there way to set up the alias name for the AVG column in the QueryBuilder?

I can not find answer in the docs, please help.



98.9k
Accepted
answer

You can pass an associative array using the keys as aliases:

$builder->columns(["average" => "avg(price)"])


36.0k

Thank you for the reply! I think it's should be in the documentation, here: https://docs.phalcon.io/en/latest/reference/phql.html#creating-queries-using-the-query-builder



8.1k

You can also write:

$builder->columns('avg(price) AS average')

(Or are there advantages to using the array format?)