Multi-Column Sorting Keeps Giving Errors

0

I'm trying to sort on three columns (Year DESC, Month DESC, & Day ASC) with the following code:

$Record = new Pod('bulletins');

$params = array();
$params['select'] = "*, date_format( str_to_date( t.bulletindate, '%Y-%m-%d' ) , '%Y' ) as sortYear, date_format( str_to_date( t.bulletindate, '%Y-%m-%d' ) , '%m' ) as sortMonth, date_format( str_to_date( t.bulletindate, '%Y-%m-%d' ) , '%d' ) as sortDay";
$params['search'] = false;
$params['orderby'] = "sortYear DESC, sortMonth DESC, sortDay ASC";
$params['limit'] = -1;

$Record->findRecords($params);

The sort will work when I only have one 'orderby' column, otherwise I get this error:

Response: Unknown column 't.sortYear' in 'order clause'

Any ideas?

asked May 5 '11 at 7:39

ckpicker

20

add comment
enter at least 15 characters

2 Answers

1

You can't use field aliases in ORDER BY, unless you're selecting something specifically, you shouldn't even be using the 'select' parameter.

Why not just use this as your orderby:

$params['orderby'] = "date_format( str_to_date( t.bulletindate, '%Y-%m-%d' ) , '%Y' ) DESC, date_format( str_to_date( t.bulletindate, '%Y-%m-%d' ) , '%m' ) DESC, date_format( str_to_date( t.bulletindate, '%Y-%m-%d' ) , '%d' ) ASC"

answered May 6 '11 at 12:59

sc0ttkclark

2936

add comment
enter at least 15 characters
0

Thanks so much Scott!

answered May 6 '11 at 4:06

ckpicker

20

add comment
enter at least 15 characters