开发者

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

开发者 https://www.devze.com 2023-02-12 23:37 出处:网络
I have a set of id\'s to select, so I request: $ids = array( 1, 2, 3, 4, 5 ); $q = DB::select(\'field1\', \'fiel开发者_运维知识库d2\', \'field3\')->

I have a set of id's to select, so I request:

$ids = array( 1, 2, 3, 4, 5 );
$q = DB::select('field1', 'fiel开发者_运维知识库d2', 'field3')->
     from('work')->
     where('field1', 'in', $ids)->execute();

How can I sort them in my custom order, like MySQL's 'ORDER BY Field' do?


Check out DB::Expr

You can use it like so:

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

Note, you'll have to manually escape the contents

0

精彩评论

暂无评论...
验证码 换一张
取 消