You can use an Expression object to represent function calls within selected 
columns:

use Zend\Db\Sql\Expression;
/* ... */
$select->columns(array('num' => new Expression('COUNT(DISTINCT(?))', 
array('*'), array(Expression::TYPE_IDENTIFIER))));

(There may be a more efficient syntax -- but hopefully this gives you an avenue 
to start exploring).

- Demian

> -----Original Message-----
> From: tm8747a [mailto:[email protected]]
> Sent: Monday, December 10, 2012 3:26 PM
> To: [email protected]
> Subject: [fw-general] Zend Framework 2 and SELECT count(*) query
> 
> I'm trying to do a query like this using Zend Framework 2:
> 
> SELECT count(*) as num FROM mytable
> 
> Here's the code I'm using to build my select statement (bear in mind I've
> imported the necessary classes):
> 
> $select = new Select();
> $select->from('mytable')
>        ->columns(array('num'=>'count(*)'), false);
> 
> This code doesn't work because the resulting query is as follows:
> 
> SELECT [count(*)] AS [num] FROM [mytable]
> 
> ...which throws the following error:
> 
> Invalid column name 'count(*)'
> 
> This is caused by the square brackets around count(*). How can I get this to
> work properly, basically to have count(*) instead of [count(*)] in the SQL.
> Also, I know that you can do it with just a regular query, but I need this
> to work with the Select object. As far as I know, this used to work with the
> previous versions of Zend, I've seen plenty of solutions for those, but
> nothing for Zend Framework 2.
> 
> 
> 
> 
> --
> View this message in context: http://zend-framework-
> community.634137.n4.nabble.com/Zend-Framework-2-and-SELECT-count-query-
> tp4658383.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
> 
> --
> List: [email protected]
> Info: http://framework.zend.com/archives
> Unsubscribe: [email protected]
> 


--
List: [email protected]
Info: http://framework.zend.com/archives
Unsubscribe: [email protected]


Reply via email to