Oct 28, 2013; 15:54
Fletcher Sandbeck
MySQL Sort in Explicit Order
I=92m not sure I=92ve seen this before, but I=92ve wanted it several times. You can sort a small set of records in an explicit order in MySQL using code like this. Assume the key field is a simple integer named ID and I want to sort the records in order 5,4,1,2,3.
SELECT * FROM mytable WHERE =85 ORDER BY id=5 DESC, id=4 DESC, id=1 DESC, id=2 DESC, id=3 DESC;
The order by clause gets quite long for more than ten or twenty records, but for reasonable cases this can simplify things on Lasso=92s end by producing the records in the right order from MySQL rather than looping through the found set to display them in the right order.
var(=91indices' = array(5,4,1,2,3));
var('sql' = 'SELECT * FROM mytable WHERE id IN (' + $indices->join(=91,') + ')=92);
$sql->append(' ORDER BY')
iterate($indices,var(=91i'))
$sql->append(' id=' + $i + ' DESC,=92);
/iterate;
$sql->removetrailing(',=92);
inline(-database==91mydatabase', -table==91mytable, -sql=$sql, -novaluelists, -maxrecords='all=92);
=85
/inline;
Hope this helps,
[fletcher]
#############################################################
This message is sent to you because you are subscribed to
the mailing list Lasso
Lasso@lists.lassosoft.com
To unsubscribe, E-mail to: <Lasso-unsubscribe@lists.lassosoft.com>
Send administrative queries to <Lasso-request@lists.lassosoft.com>
Oct 28, 2013; 20:15
Justin Dennis
Re: MySQL Sort in Explicit Order
Oct 28, 2013; 19:00
Fletcher Sandbeck
Re: MySQL Sort in Explicit Order
Oct 28, 2013; 20:06
deco rior
Re: MySQL Sort in Explicit Order