-6ren">
gpt4 book ai didi

sql-order-by - Kohana 数据库查询生成器自定义排序顺序 ("ORDER BY Field (id, 1, 3, 2)"在 MySQL 中)

转载 作者:行者123 更新时间:2023-12-04 05:29:35 25 4
gpt4 key购买 nike

我有一组 id 可供选择,所以我请求:

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

如何按照我的自定义顺序对它们进行排序,就像 MySQL 的“ORDER BY Field”那样?

最佳答案

退房 DB::Expr

你可以像这样使用它:

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

请注意,您必须手动转义内容

关于sql-order-by - Kohana 数据库查询生成器自定义排序顺序 ("ORDER BY Field (id, 1, 3, 2)"在 MySQL 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5104928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com