Kohana Database Query Builder custom sort order ("ORDER BY FIELD (id, 1, 3, 2)" in MySQL)

I have a set of identifiers to choose from, so I request:

$ids = array( 1, 2, 3, 4, 5 ); $q = DB::select('field1', 'field2', 'field3')-> from('work')-> where('field1', 'in', $ids)->execute(); 

How can I sort them in my user order, for example mysql 'ORDER BY FIELD' do?

+4
source share
1 answer

Check out DB :: Expr

You can use it like this:

 ->order_by(DB::Expr('FIELD(`field`, 3,1,2)')) 

Please note: you will have to manually avoid content

+7
source

Source: https://habr.com/ru/post/1341133/


All Articles