dpepbjp126917 2018-06-29 13:58
浏览 47
已采纳

是否可以对子查询创建的变量运行连接?

Right now I am running a sub-query to get the most recent status for a server, this sub-query is returning through the variable last_status.

 //This is ran when WithLastStatusDate() is called
$query->addSubSelect('last_status', ServerStatus::select('status_id')
    ->whereRaw('server_id = servers.id')
    ->latest()
);

$servers = Server::WithLastStatusDate()
    ->OrderBy('servers.id', 'desc')
    ->where('servers.isPublic', '=', 1)
    ->get(); 

What I am trying to do now is do a join on this so that it gives me the actual name of the status based on the result of this query in the statuses table. I have tried to do a simple left join but am getting the error that the last_status column isn't found.

$servers = Server::WithLastStatusDate()
    ->OrderBy('servers.id', 'desc')
    ->where('servers.isPublic', '=', 1)
    ->leftjoin('statuses','servers.last_status', '=', 'statuses.id')
    ->get(); 

Can anyone point me in the right direction on how to accomplish this?

EDIT::

Server Table:

 Schema::create('servers', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('name');
            $table->string('url');
            $table->boolean('isPublic');
            $table->timestamps();
        });

Server_statuses Table:

Schema::create('server_statuses', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->integer('server_id')->unsigned();
            $table->foreign('server_id')->references('id')->on('servers')->onDelete('cascade');
            $table->integer('status_id')->unsigned();
            $table->foreign('status_id')->references('id')->on('statuses');
            $table->timestamps();
        });

statuses table:

Schema::create('statuses', function (Blueprint $table) {
    $table->engine = 'InnoDB';
    $table->increments('id');
    $table->string('key');
    $table->string('status');
    $table->timestamps();
});

What $servers looks like after sub-query:

enter image description here

Raw SQL of query:

select `servers`.*, (select `status_id` from `server_statuses` where server_id = servers.id order by `created_at` desc limit 1) as `last_status` from `servers` where `servers`.`isPublic` = '1' order by `servers`.`id` desc

EDIT 2::

    $servers = DB::table('servers as sv')
        ->join('server_statuses as ss', 'sv.id', '=', 'ss.server_id')
        ->join('statuses as st', 'ss.status_id', '=', 'st.id')
        ->WithLastStatus()
        ->OrderBy('servers.id', 'desc')
        ->where('servers.isPublic', '=', 1)
        ->get();
  • 写回答

3条回答 默认 最新

  • doulianxing4015 2018-06-29 17:59
    关注

    Combine LEFT JOINs with a subquery WHERE clause:

    $servers = Server::select('servers.*', 'statuses.status as status_name')
        ->leftJoin('server_statuses', function($join) {
            $join->on('server_statuses.server_id', '=', 'servers.id')
                ->where('server_statuses.id', function($query) {
                    $query->select('id')
                        ->from('server_statuses')
                        ->whereColumn('server_id', 'servers.id')
                        ->latest()
                        ->limit(1);
                });
        })
        ->leftJoin('statuses', 'statuses.id', '=', 'server_statuses.status_id')
        ->where('servers.isPublic', '=', 1)
        ->orderBy('servers.id', 'desc')
        ->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算