【问题标题】:How to Write subquery in laravel?如何在 laravel 中编写子查询?
【发布时间】:2017-05-25 21:11:34
【问题描述】:
$get_duplicate=DB::select('select * from student_info a join ( select first_name, last_name, dob  from student_info group by first_name, last_name, dob having count(*) > 1 ) b on a.first_name = b.first_name and a.last_name = b.last_name and a.dob = b.dob join participant_info on participant_info.id = a.participant_id order by a.first_name ASC');

如何将此查询转换为 Eloquent 格式。

这是我的模型。

<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class StudentInfo extends Model
{
    //
    public $timestamps = true;
    protected $table = 'student_info';
    protected $guarded = array();
    public function student_participant_det(){
        return $this->hasOne('App\Model\ParticipantInfo', 'id','participant_id');

    }
}

请帮我把它转换成 Eloquent 格式

【问题讨论】:

  • 让我们看看您的模型及其关联关系。
  • 'StudentInfo' 是一个模型和 hasOne('App\Model\ParticipantInfo', 'id','participant_id'); } 我可以通过这个函数获取相关的参与者信息
  • 请在您的问题中输入任何相关代码,而不是在 cmets 中。
  • 现在你能帮帮我吗??
  • 你能不能也显示ParticpantInfo

标签: php mysql laravel subquery laravel-query-builder


【解决方案1】:

我已经编辑了之前的代码。请试试这个。

$get_duplicate=DB::table('student_info as a')
->join(DB::raw('(SELECT first_name, last_name, dob  FROM student_info group by first_name, last_name, dob having count(*) > 1 b'), function($join) {
    $join->on('a.first_name', '=', 'b.first_name');
    $join->on('a.last_name', '=', 'b.last_name');
    $join->on('a.dob', '=', 'b.dob'); })
->join('participant_info','participant_info.id', '=', 'a.participant_id')
->orderBy('a.first_name', 'ASC') ->get();

【讨论】:

  • 谢谢@mith,我已经找到了解决方案....感谢您的帮助:)
猜你喜欢
  • 2022-01-03
  • 2021-03-30
  • 2017-11-28
  • 2019-07-13
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 2020-06-15
相关资源
最近更新 更多