Thursday, 15 May 2014

php - Mysql query, custom order -



php - Mysql query, custom order -

i have next mysql query:

select col1, col2, col3 guests active = 1 , guestid in (233, 255, 254, 226, 182, 231, 113, 121)

when results however, them ordered guestid (which indexed) in ascending order (starting 121 , ending 255). need show results on website in specific order. there way can bypass default order?

thanks.

you have specify custom order in order clause:

select col1, col2, col3 guests active = 1 , guestid in (233, 255, 254, 226, 182, 231, 113, 121) order find_in_set(guestid, '233,255,254,226,182,231,113,121');

the builtin find_in_set() function returns integer position first argument finds match in list. note list single string argument containin comma-separated list, whereas in() predicate needs variable number of arguments. iirc there must no spaces in list give find_in_set().

php mysql select order

No comments:

Post a Comment