按公司排序不工作。请帮助我

时间:2018-04-05 05:36:23

标签: php mysql laravel

  

这是我的数据库查询,当我使用 sortby公司时它不起作用我有48000条记录,但它只显示18000条记录。 Sortby状态,联系人,城市都工作正常。任何帮助都会很感激

public static function retreiveCandidates($status,$alpha=NULL,$dash=NULL,$statusid,$sortby=NULL){
    DB::enableQueryLog();

    $query = DB::table('candidates')->select('candidates.candidateid','candidates.firstname','candidates.lastname','candidates.imageext','candidates.middlename','candidates.personal_email','candidates.city','candidates.mobile','candidates.title_id','candidates.status','candidates.state','candidates.isverified','candidates.candidate_statusid','candidates.sendinvitation','candidates.ismerged','candidates.imagekey','candidates.candidatekey')->where('candidates.status',$status);
    if($alpha!=''){
        $query = $query->where('candidates.firstname', 'like', $alpha . '%');
    }
    if($statusid !=''){
        $query = $query->where('candidates.candidate_statusid',$statusid);
    }
    if($dash !=''){
        $result = $query->orderBy('candidates.created_at', 'desc')->limit(7)->get();
    }

    if($sortby == NULL){
        $query = $query->orderBy('candidates.firstname', 'asc') ;
    }elseif($sortby == 'company'){
        $query = $query->join('candidates_company','candidates_company.candidateid','=','candidates.candidateid')->join('company','company.companyid','=','candidates_company.companyid')->where('candidates_company.status','1')->groupBy('candidates.candidateid')->orderBy('company.company', 'asc')->orderBy('candidates.candidateid', 'asc')  ;
    }elseif($sortby == 'status'){
           $query = $query->join('candidate_status','candidate_status.statusid','=','candidates.candidate_statusid')->orderBy('candidate_status.statusorder', 'asc') ;  
    }elseif($sortby == 'contact'){
        $query = $query->orderBy('candidates.firstname', 'asc');            
    }elseif( $sortby == 'city'){            
        $query = $query->orderBy('candidates.city', 'asc') ;
    }elseif( $sortby == 'state'){           
        $query = $query->orderBy('candidates.state', 'asc') ;
    }

    $result = $query->paginate(PAGELIMIT);
    $d= DB::getQueryLog();
    return $result;     
}
  

打印dbquery时得到的内容

    Array
(
    [0] => Array
        (
            [query] => select count(*) as aggregate from `rsi_candidates` inner join `rsi_candidates_company` on `rsi_candidates_company`.`candidateid` = `rsi_candidates`.`candidateid` inner join `rsi_company` on `rsi_company`.`companyid` = `rsi_candidates_company`.`companyid` where `rsi_candidates`.`status` = ? and `rsi_candidates_company`.`status` = ? group by `rsi_candidates`.`candidateid`
            [bindings] => Array
                (
                    [0] => 1
                    [1] => 1
                )

            [time] => 1036.83
        )

    [1] => Array
        (
            [query] => select `rsi_candidates`.`candidateid`, `rsi_candidates`.`firstname`, `rsi_candidates`.`lastname`, `rsi_candidates`.`imageext`, `rsi_candidates`.`middlename`, `rsi_candidates`.`personal_email`, `rsi_candidates`.`city`, `rsi_candidates`.`mobile`, `rsi_candidates`.`title_id`, `rsi_candidates`.`status`, `rsi_candidates`.`state`, `rsi_candidates`.`isverified`, `rsi_candidates`.`candidate_statusid`, `rsi_candidates`.`sendinvitation`, `rsi_candidates`.`ismerged`, `rsi_candidates`.`imagekey`, `rsi_candidates`.`candidatekey` from `rsi_candidates` inner join `rsi_candidates_company` on `rsi_candidates_company`.`candidateid` = `rsi_candidates`.`candidateid` inner join `rsi_company` on `rsi_company`.`companyid` = `rsi_candidates_company`.`companyid` where `rsi_candidates`.`status` = ? and `rsi_candidates_company`.`status` = ? group by `rsi_candidates`.`candidateid` order by `rsi_company`.`company` asc, `rsi_candidates`.`candidateid` asc limit 10 offset 0
            [bindings] => Array
                (
                    [0] => 1
                    [1] => 1
                )

            [time] => 454.11
        )

)

2 个答案:

答案 0 :(得分:1)

对未来参考的建议,使用" ==="而不是" ==",第二个将在一些最尴尬的案例中产生真实。

进入问题,您的搜索只有

  

其中(' candidates_company.status'' 1&#39)

可能会缩小搜索结果的范围,但实际上并没有看到我无法确定问题的表格,但我无法看到除此之外的任何其他内容正在检索的结果数量。

答案 1 :(得分:0)

创建候选人模型

php artisan make:model Candidate

这将在您的app文件夹中创建一个模型文件candidate.php。该文件看起来像

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Candidate extends Model
{
 protected $table = 'candidate'; //give your table name here
}

现在您可以在控制器中访问您的候选模型,如

use App\Candidate;
class CandidateController extends Controller
{
 $candidates= App\Candidate::orderBy('company')->get(); //select by orderby
}

希望这有帮助