TP5根据条件IN里面的查询顺序进行排名

时间:2025-03-03 10:57:32

使用TP的Expression类

解决方法:

     1.数组ID

     array(5) {[0]=>int(196) [1]=>int(197) [2]=>int(198) [3]=>int(200) [4]=>int(201)}

     2.返回由数组元素组合成的字符串(逗号分隔)

     $str = implode(',',数组ID);

     3.TP5的排序写法

1

2

$exp new \think\db\Expression('field(id,'.$str.')');

$result = Db::name('')->where('id','IN',数组ID)->order($exp)->select();