主要想实现的功能是想当2个表对应的id相同时,赋予主表一个参数并定义值,并且根据这个值去按顺序排序,最开始我使用的方法是各查出2个表然后比较id相同赋值,后来排序的时候才发现在分页的条件下只能对当前页进行排序,这与自己的功能不符,于是采用了子查询,先对另一个表进行条件查询,然后再关联主表,然后排序就可以了,以下是代码:
$subOrderSql = Inv::where('add_time','between',[strtotime(date("Y-m-01", $task->add_time)),strtotime(date('Y-m-t', $task->add_time))])
->where('task_id',$task->id)->where('type',2)->group('pro_id')->buildSql();
$wheres['ware_status'] = 0;
$wareData = Ware::alias('w')
->join('pro p','p.id = w.pro_id')
->join([$subOrderSql => 'so'], 'so.pro_id = w.pro_id', 'left')
->field('p.image,p.name,p.price,p.sn,w.id ware_id,p.id pro_id,so.call_out_nums,so.format_nums,if(so.call_out_nums != so.format_nums,1,0) as difference_status,if(so.type = 2,1,0) as inv_status')
->where($wheres)
->order('inve_status asc,w.id desc')
->paginate(10);
定义别名:inve_status 进行排序,到此功能实现