MAX() in query

2010-12-29 Thread vasquinho
I have a table of a number of issues
id, number, year, user_id
1 | 1 | 2009 | 1
2 | 2 | 2009 | 1
3 | 3 | 2009 | 1
4 | 1 | 2010 | 1
5 | 2 | 2010 | 1

and for each user_id I want to retrieve the greatest number grouped by
year.

I could solve my problem with the following query:

SELECT `Issue`.`id` AS `id`, `Issue`.`number` AS `number,
`Issue`.`year` AS `year, `Issue`.`user_id` AS `user_id` FROM `issues`
AS `Issue`
INNER JOIN (
SELECT MAX(`Issue`.`number`) AS `number`, `Issue`.`year` AS `year`
FROM `issues` AS `Issue`
WHERE `Issue`.`user_id` = 1
GROUP BY `Issue`.`year`
) AS `JoinedTable`
ON ( `Issue`.`number` = `JoinedTable`.`number`
AND `Issue`.`year` = `JoinedTable`.`year`
);

But I'm having some trouble translating that sql to cakephp so it
integrates with paginator() and find()

Could anybody give me some hints on how to solve this?

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: MAX() in query

2010-12-29 Thread nurvzy
http://book.cakephp.org/view/1030/Complex-Find-Conditions

That should help you, read through how to build complex conditions
with subqueries and build the conditions array described.  Once you
have your conditions array just pass it to $this-
paginate($conditions);  You should be all set.

Hope that helps,
Nick

On Dec 29, 10:38 am, vasquinho jvrodrig...@gmail.com wrote:
 I have a table of a number of issues
 id, number, year, user_id
 1 | 1 | 2009 | 1
 2 | 2 | 2009 | 1
 3 | 3 | 2009 | 1
 4 | 1 | 2010 | 1
 5 | 2 | 2010 | 1

 and for each user_id I want to retrieve the greatest number grouped by
 year.

 I could solve my problem with the following query:

 SELECT `Issue`.`id` AS `id`, `Issue`.`number` AS `number,
 `Issue`.`year` AS `year, `Issue`.`user_id` AS `user_id` FROM `issues`
 AS `Issue`
 INNER JOIN (
 SELECT MAX(`Issue`.`number`) AS `number`, `Issue`.`year` AS `year`
 FROM `issues` AS `Issue`
 WHERE `Issue`.`user_id` = 1
 GROUP BY `Issue`.`year`
 ) AS `JoinedTable`
 ON ( `Issue`.`number` = `JoinedTable`.`number`
 AND `Issue`.`year` = `JoinedTable`.`year`
 );

 But I'm having some trouble translating that sql to cakephp so it
 integrates with paginator() and find()

 Could anybody give me some hints on how to solve this?

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en