<![CDATA[ Query with alias in where clause - My Forums]]> https://forums.modx.com/thread/?thread=99231 <![CDATA[Query with alias in where clause]]> https://forums.modx.com/thread/99231/query-with-alias-in-where-clause#dis-post-536637
For instance:
$query = $modx->newQuery('request');
$query->select(array(
    'id',
    'lastupdate' ,
    'DATEDIFF(NOW(),lastupdate) AS Age'
));
$query->where(array(
    'Age:>=' => 7
));


I tried this as well:
$query->where(array(
    'DATEDIFF(NOW(),lastupdate):>=' => 7
));


In both causes I get "an unknown column" error.

I hope someone can help me. Thanks in advance.]]>
egraaf Jan 05, 2016, 07:24 AM https://forums.modx.com/thread/99231/query-with-alias-in-where-clause#dis-post-536637
<![CDATA[Re: Query with alias in where clause (Best Answer)]]> https://forums.modx.com/thread/99231/query-with-alias-in-where-clause#dis-post-537712
$query = $modx->newQuery('request');
$query->select(array(
    'id',
    'lastupdate' ,
    'DATEDIFF(NOW(),lastupdate) AS Age'
));

$query->having('Age >= 7');



or

$query = $modx->newQuery('request');
$query->select(array(
    'id',
    'lastupdate' ,
    'DATEDIFF(NOW(),lastupdate) AS Age'
));

$query->where('DATEDIFF(NOW(),lastupdate)  >= 7');

]]>
Bruno17 Feb 01, 2016, 06:30 AM https://forums.modx.com/thread/99231/query-with-alias-in-where-clause#dis-post-537712
<![CDATA[Re: Query with alias in where clause]]> https://forums.modx.com/thread/99231/query-with-alias-in-where-clause#dis-post-537711 Quote from: BobRay at Jan 06, 2016, 06:29 PM
Try removing this line:

'DATEDIFF(NOW(),lastupdate) AS Age'

I think that will work but then I loose the column Age in the query output.

Is this what you mean?

$query = $modx->newQuery('request');
$query->select(array(
    'id',
    'lastupdate'
));
$query->where(array(
    'DATEDIFF(NOW(),lastupdate):>=' => 7
));


I need the column "Age" to process the output. I have written a snippet:

$requests = $modx->getCollection('request',$query);
foreach ($requests as $r)
{
  	$request=$r->toArray();

  	if ($request[Age]>7)
        ......


This is Ok for the moment. The collection contains too much elements, but that's all.
I was wondering if there was a more efficient way to get the records, but it seems to be a problem using an alias.
]]>
egraaf Feb 01, 2016, 06:22 AM https://forums.modx.com/thread/99231/query-with-alias-in-where-clause#dis-post-537711
<![CDATA[Re: Query with alias in where clause]]> https://forums.modx.com/thread/99231/query-with-alias-in-where-clause#dis-post-536703
'DATEDIFF(NOW(),lastupdate) AS Age'
]]>
BobRay Jan 06, 2016, 12:29 PM https://forums.modx.com/thread/99231/query-with-alias-in-where-clause#dis-post-536703
<![CDATA[Re: Query with alias in where clause]]> https://forums.modx.com/thread/99231/query-with-alias-in-where-clause#dis-post-536666 Quote from: BobRay at Jan 05, 2016, 08:21 PM
How about something like this:

$cutoff = strtotime(-1 week); /* 7 days ago */

$query->where(array(
     (string) $cutoff . ':>=' => 'lastupdate'
));

The problem is that de resulting query looks like this:
SELECT `id`, `lastupdate`, `state`, `file`, DATEDIFF(NOW(),lastupdate) AS Age, OCTET_LENGTH(file) AS FileSize FROM `modx_my_requests` AS `myRequest` WHERE ( `myRequest`.`state` = 6 AND `myRequest`.`1451466840` >= 'lastupdate' ) ORDER BY lastupdate ASC

The part "`myRequest`.`1451466840`" results in an error.]]>
egraaf Jan 06, 2016, 03:19 AM https://forums.modx.com/thread/99231/query-with-alias-in-where-clause#dis-post-536666
<![CDATA[Re: Query with alias in where clause]]> https://forums.modx.com/thread/99231/query-with-alias-in-where-clause#dis-post-536655
$cutoff = strtotime(-1 week); /* 7 days ago */

$query->where(array(
     (string) $cutoff . ':>=' => 'lastupdate'
));
]]>
BobRay Jan 05, 2016, 02:21 PM https://forums.modx.com/thread/99231/query-with-alias-in-where-clause#dis-post-536655