为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel Eloquent - 按每种类型选择最大的 - join、max()

Laravel Eloquent - 按每种类型选择最大的 - join、max()

PHP
UYOU 2022-10-14 16:29:45
我还在学习laravel和雄辩,我有一个小问题......我有三个表:Schema::create('fishes', function (Blueprint $table) {                $table->bigIncrements('id');                $table->bigInteger('user_id')->unsigned();                $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');                $table->bigInteger('type_id')->unsigned();                $table->foreign('type_id')->references('id')->on('fish_types')->onDelete('cascade');                $table->float('length');});Schema::create('fish_types', function (Blueprint $table) {                $table->bigIncrements('id');                $table->string('name')->unique();                $table->string('name_raw')->unique();});Schema::create('photos', function (Blueprint $table) {                $table->bigIncrements('id');                $table->string('photoable_type');                $table->string('photoable_id');                $table->string('path');});我有模型鱼以及与鱼类型和照片的关系。它的工作,一切都很好,例如:$f = Fish::with('photos', 'fishery', 'type')->when($filters['userId'], function ($query) use ($filters) {    return $query->where('user_id', $filters['userId']);});但我想从 db 获得属于用户的每种类型中最长的鱼,当然还有照片(急切加载)。我有 mysql 问题(是的,这很糟糕,但我加入 eloquent 没有用:():$sql = "SELECT id  FROM fishes f1  JOIN      ( SELECT type_id            , MAX(`length`) AS pb          FROM fishes          where user_id = 6          GROUP BY type_id     ) AS f2    ON f1.type_id = f2.type_id    and f2.pb = f1.length  where f1.user_id = 6";所以我有鱼的身份证 - 但下一步是什么?相同的查询“whereIn(Column_name, Array)”?$sth = DB::getPdo()->prepare($sql);$sth->execute();$quy = $sth->fetchAll(\PDO::FETCH_COLUMN, 0);$f = Fish::with('photos', 'fishery', 'type')          ->where('user_id', 6)          ->whereIn('id', $quy)->get();缩短的鱼模型:class Fish extends Model{    public function type()    {        return $this->belongsTo('App\Models\FishType');    }    public function fishery()    {        return $this->belongsTo('App\Models\Fishery');    }
查看完整描述

1 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

您正在执行两个查询,但这可以在一个查询中完成。尝试这个


Fish::with('photos', 'fishery', 'type')

    ->join(DB::raw('( SELECT type_id

            , MAX(`length`) AS pb

          FROM fishes

          where user_id = 6

          GROUP BY type_id

     ) AS f2'),function($join){

    $join->on('fishes.type_id','=','f2.type_id')

         ->on('fishes.length','=','f2.pb');

    })

    ->where('user_id', 6)->get();

代替


$sql = "

SELECT id

  FROM fishes f1

  JOIN **strong text**

     ( SELECT type_id

            , MAX(`length`) AS pb

          FROM fishes

          where user_id = 6

          GROUP BY type_id

     ) AS f2

    ON f1.type_id = f2.type_id 

   and f2.pb = f1.length 

 where f1.user_id = 6

";


$sth = DB::getPdo()->prepare($sql);

$sth->execute();

$quy = $sth->fetchAll(\PDO::FETCH_COLUMN, 0);

$f = Fish::with('photos', 'fishery', 'type')

          ->where('user_id', 6)

          ->whereIn('id', $quy)->get();


查看完整回答
反对 回复 2022-10-14
  • 1 回答
  • 0 关注
  • 167 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号