做项目遇到个需求,需要对结果集中的数据进行指定规则的顺序排列。
例如,用户状态有四种:

0=>未激活;1=>正常;2=>禁用;3=>软删除

现在的需求是,我要按照:正常->未激活->禁用->删除;这个顺序来进行排序,同时按照注册时间降序,网上查了很多资料,国内提到这个的很少,在stackOverFlow上找到了答案!
先上解决方案:

public function index($customer_type = null) {
$search = request('search');
$perPage = request('perPage') ? request('perPage') : 10;
$customer_type = $customer_type ? $customer_type : request('customer_type');
// \DB::enableQueryLog();
$data = Customer::select(['id', 'email', 'user_name', 'nick_name', 'status', 'phone', 'create_time'])
->where('customer_type', '=', $customer_type)
->where(function ($query) use ($search) {
if ($search) {
$query->where('user_name', 'like binary', '%' . $search . '%')
->orWhere('nick_name', 'like binary', '%' . $search . '%')
->orWhere('phone', 'like binary', '%' . $search . '%')
->orWhere('email', 'like binary', '%' . $search . '%');
}
})
->orderByRaw("FIELD(status, " . implode(", ", [1, 2, 0, 3, 4]) . ")")
->orderBy('create_time', 'desc')
->paginate($perPage);
// $query = \DB::getQueryLog();
// dd($data);
//追加额外参数,例如搜索条件
$appendData = $data->appends(array(
'search' => $search,
'perPage' => $perPage,
));
return view('admin/customer/customerList', compact('data'));
}

  打印出来的sql语句如下:

参考了以下链接:
https://stackoverflow.com/questions/42068986/laravel-weird-behavior-orderbyrawfield

https://stackoverflow.com/questions/34244455/how-to-use-not-find-in-set-in-laravel-5-1

https://stackoverflow.com/questions/35594450/find-in-set-in-laravel-example/35594503

find_in_set 复杂应用:

public function get_teacher_list($timeType, $name, $perPage = 10, $personality = 0, $teachingStyle = 0, $ageType = 0)
{
// \DB::enableQueryLog();
$result_data = DB::table('teacher_info as ti')
->select('ti.*')
->join('customer', 'customer.id', '=', 'ti.customer_id')
->where(function ($query) use ($personality) {
if (trim($personality)) {
$query->whereRaw("find_in_set($personality,ti.label_ids)");
}
})
->where(function ($query) use ($teachingStyle) {
if (trim($teachingStyle)) {
$query->whereRaw("find_in_set($teachingStyle,ti.label_ids)");
}
})
->where(function ($query) use ($ageType) {
if (trim($ageType)) {
$ageType = explode('-', $ageType);
$query->whereRaw("DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthday)), '%Y')+0 between $ageType[0] and $ageType[1]");
}
})
->where(function ($query) use ($timeType) {
//1本周,2下周
if ($timeType == 1) {
$query->where('ti.can_appointment_1', 1);
} elseif ($timeType == 2) {
$query->where('ti.can_appointment_2', 1);
} else {
$query->where('ti.can_appointment_1', '>', 0)
->orWhere('ti.can_appointment_2', '>', 0);
}
})
->where(function ($query) use ($name) {
if (trim($name)) {
$query->where('ti.chinese_name', 'like', '%' . $name . '%')
->orWhere('ti.english_name', 'like', '%' . $name . '%');
}
})
->where('ti.status', 1)
->orderBy('ti.total_teach_num', 'desc')
->orderBy('ti.total_star_num', 'desc')
->orderBy('ti.satisfaction', 'desc')
->orderBy('ti.comment_num', 'desc')
->orderBy('ti.english_name', 'asc')
->paginate($perPage);
// dd($result_data, \DB::getQueryLog()); return $result_data;
}

  专门拿出来看一下:

$ids = array(1,17,2);

$ids_ordered = implode(',', $ids);

$items = User::whereIn('id', $ids)
->orderByRaw(DB::raw("FIELD(id, $ids_ordered)"))
->get();

  转载:https://blog.csdn.net/zhezhebie/article/details/78357354

最新文章

  1. Maven打包程序
  2. spring @ModelAttribute 注解
  3. 攻城狮在路上(叁)Linux(二十一)--- linux磁盘检查 fsck \ badblocks
  4. jQuery基础之(三)jQuery功能函数前缀及与window.onload冲突
  5. 2016年6月25日 星期六 --出埃及记 Exodus 14:22
  6. GetMemory
  7. Android访问C#的WebService要注意的问题
  8. NancyFx 2.0的开源框架的使用-Forms
  9. QT 5.9版本 使用MSVC2015编译时出现中文字符乱码问题的解决方法
  10. HBase JavaAPI
  11. leetcode题解 9. Palindrome Number
  12. H264--2--语法及结构
  13. Python全栈-库的操作
  14. laravel 连表查询数据库
  15. 第一篇:打造专属开发工具Eclipse篇
  16. tomcat管理员在远程(不同)机器上访问管理页面
  17. iOS求职之C语言面试题
  18. asp web api json 序列化后 把私有字段信息也返回了解决办法
  19. Webwork 学习笔记
  20. Hello Vizhub

热门文章

  1. apache备忘录
  2. Vim中复制粘贴缩进错乱问题的解决方案
  3. centos8下jdk13和tomcat9的安装
  4. KVM命令记录
  5. Mount Windows (CIFS) shares on Linux with credentials in a secure way
  6. STM32WB 振荡器与时钟
  7. 1.RPC原理学习
  8. C++第三次作业--作用域
  9. 本地phpmyadmin 访问远程数据库服务器
  10. li每三个换行