【问题标题】:MySQL/Eloquent force id by left join even if nullMySQL/Eloquent force id by left join 即使为空
【发布时间】:2019-02-13 22:39:48
【问题描述】:

我用 Eloquent 写了这个查询:

    return TypeModel::with(['apt' => function($q) use ($id){
        $q->leftJoin('build_apt', function($join) use($id){
            $join->on('build_apt.id_apt', '=', 'apt.id_apt');
            $join->on('build_apt.id_build', '=', DB::raw($id->id_build));
        })
    }])
    ->get()
    ->toJson();

等效 SQL:

SELECT * 
FROM `apt` 
LEFT JOIN `build_apt` 
    ON  `build_apt`.`id_apt` = `apt`.`id_apt` 
    AND `build_apt`.`id_build` = 1 
WHERE `apt`.`id_typeapt` in (1, 2, 3, 4, 5)

我有我需要的结果,除了一件事,给定的 id 是 null :

[
    {"id_typeapt":1,"apt":[
        {"id_apt":null,"image_apt":"apt_1.png"}, 
        {"id_aptitude":null,"image_apt":"apt_2.png"}
    ]
]

如何强制它从“apt”表中查找 id 而结果不给我“null”?

谢谢!

编辑:Where 子句来自 with

public function apt(){
    return $this->hasMany(AptModel::class, 'id_typeapt', 'id_typeapt');
}

EDIT2:

id_apt 被另一个id_apt 粉碎了,只需简单的重命名我就可以检索到 id:

    return TypeModel::with(['apt' => function($q) use ($id){
        $q->leftJoin('build_apt', function($join) use($id){
            $join->on('build_apt.id_apt', '=', 'apt.id_apt');
            $join->on('build_apt.id_build', '=', DB::raw($id->id_build));
        })
    }])->select(DB::raw("*, apt.id_apt as id_apt);
  }])

【问题讨论】:

  • WHERE 子句从何而来?请展示你所有的 Eloquent 代码。
  • 来自with。我更新了帖子
  • 从 SQL 的角度来看,您需要将WHERE 子句移动到LEFT JOIN 上的ON 条件build_apt
  • select * from apt left join build_apt on build_apt.id_apt = apt.id_apt and build_apt.id_build = 1 and @987654 id_typeapt in (1, 2, 3, 4, 5)

标签: php mysql sql laravel eloquent


【解决方案1】:

您正在使用LEFT JOIN,这是您想要的,但是您在WHERE 子句中对连接表有条件。这实际上会将您的LEFT JOIN 转换为INNER JOIN,因为不匹配的记录将被WHERE 子句过滤掉。

您需要调整 Eloquent 代码以生成以下 SQL 查询:

SELECT * 
FROM `apt` 
LEFT JOIN `build_apt` 
    ON  `build_apt`.`id_apt` = `apt`.`id_apt` 
    AND `build_apt`.`id_build` = 1 
    AND `apt`.`id_typeapt` in (1, 2, 3, 4, 5)

【讨论】:

  • 在带有此查询的 MySQL 中,列 id_apt 仍然是 null,因为在“build_apt”中没有匹配项。另一列正在粉碎实际需要的id_apt。现在可以重命名了。
猜你喜欢
  • 1970-01-01
  • 2020-05-17
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多