ThinkPHP5 order 查询自定义排序 按照查询结果排序

2023-01-1808:32:14后端程序开发Comments1,495 views字数 214阅读模式

工作中遇到 需要自定义排序的情况,步骤如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30495.html

1.首先引入Expression类文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30495.html

use think\db\Expression;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30495.html

2.排序时准备工作文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30495.html

// 自定义排序文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30495.html

$ids="1,3,2,5,7,9";文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30495.html

$exp=new Expression("field(id,$ids)");文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30495.html

// 查询时文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30495.html

$res=Db::table('table')->whereIn('id',$ids)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30495.html

->order($exp)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30495.html

->select();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30495.html

这样便可自定义排序文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30495.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/30495.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定