join 3表与laravel中的查询生成器

w1jd8yoj  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(403)

我有一个3表的问题,注册,ssiè跟踪我需要得到详细信息,从注册表对应的其他两个表
我需要根据问题从注册中获取详细信息。question\u schedul=0,ssi\u tracks.track\u first\u status
我写了一个查询,但是它说在这里找不到列,这是我的查询

$register = DB::table('registrations')
            ->join('questions', 'registrations.registration_id', '=', 'questions.question_id')
             ->join('ssi_tracks','registrations.registration_id','=','ssi_tracks.registration_id')
             ->select('address', 'model', 'chassis', 'delivery_date','ssi_tracks.track_first_status')
            ->where([["questions.question_schedul", "=", $dropselected] and ['ssi_tracks.track_first_status',0]])
             ->get();
p5cysglq

p5cysglq1#

试试这个:

$register = DB::table('registrations as R')
             ->select('R.address', 'R.model', 'R.chassis', 'R.delivery_date','S.track_first_status')
             ->join('questions as Q', 'R.registration_id', '=', 'Q.question_id')
             ->join('ssi_tracks as S','R.registration_id','=','S.registration_id')
             ->where('Q.question_schedul', '=', $dropselected)
             ->where('S.track_first_status', '=', 0)
             ->get();

请确保已使用问题表中的右侧列匹配注册id:

->join('questions as Q', 'R.registration_id', '=', 'Q.question_id')
6yt4nkrj

6yt4nkrj2#

尝试此查询:

$register = DB::table('registrations')
             ->leftJoin('questions', 'registrations.registration_id', '=', 'questions.question_id')
             ->leftJoin('ssi_tracks','registrations.registration_id','=','ssi_tracks.registration_id')
             ->select('registrations.address', 'registrations.model', 'registrations.chassis', 'registrations.delivery_date','ssi_tracks.track_first_status')
             ->where(['questions.question_schedul'=>$dropselected,'ssi_tracks.track_first_status'=>0])
             ->get();

相关问题