加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Laravel SQL查询返回错误的结果

发布时间:2020-12-14 19:40:34 所属栏目:大数据 来源:网络整理
导读:我有这个带有Laravel Query构建器的SQL查询 $this-candidate-select('candidates.*','candidates.id as candidate_id','mj.interviewScheduledDate') -join('candidates_positions','candidates_positions.candidate_id','=','candidates.id') -leftJoin(DB:
我有这个带有Laravel Query构建器的SQL查询

$this->candidate->select('candidates.*','candidates.id as candidate_id','mj.interviewScheduledDate')
            ->join('candidates_positions','candidates_positions.candidate_id','=','candidates.id')
            ->leftJoin(DB::raw("(SELECT mat1.* FROM matches AS mat1
                                    JOIN (SELECT candidate_id,MAX(id) AS id FROM matches GROUP BY candidate_id) 
                                    AS mat2 ON mat1.candidate_id = mat2.candidate_id AND mat1.id = mat2.id)
                                    AS mj"),function ($join) {
                $join->on("candidates.id","=","mj.candidate_id");
            })
            ->where(function ($query) {
                $query->where("mj.stage","<","4")
                    ->whereNull('mj.stage',"or");
            })
            ->groupBy('candidates.id')

            ->paginate(Config::get('match.pagination'));

这会返回错误的结果,其中来自同一“查询”构建器的“生成”查$candidate-> toSql()返回以下查询.甚至尝试删除group by语句.它没有帮助

SELECT
      `candidates`.*,`candidates`.`id` AS `candidate_id`,`mj`.`interviewScheduledDate`
    FROM `candidates`
      INNER JOIN `candidates_positions` ON `candidates_positions`.`candidate_id` = `candidates`.`id`
      LEFT JOIN (SELECT mat1.*
                 FROM matches AS mat1
                   JOIN (SELECT
                           candidate_id,MAX(id) AS id
                         FROM matches
                         GROUP BY candidate_id)
                     AS mat2 ON mat1.candidate_id = mat2.candidate_id AND mat1.id = mat2.id)
        AS mj ON `candidates`.`id` = `mj`.`candidate_id`
    WHERE (`mj`.`stage` < ? OR `mj`.`stage` IS NULL)
    GROUP BY `candidates`.`id`
    LIMIT 10 OFFSET 0

正确的结果

enter image description here

Laravel返回结果

enter image description here

解决方法

仔细检查如何绑定值.你可能会绑定与Laravel不同的值.

这个链接也很有用:Different results using same query with DB::raw and Eloquent

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读