简体   繁体   English

Laravel 4查询生成器:LEFT JOIN ... AND ...查询

[英]Laravel 4 Query Builder: LEFT JOIN … AND … query

I'm trying to select all results from one table and merge with another table when user ID's match. 我正在尝试从一个表中选择所有结果,并在用户ID匹配时与另一个表合并。

I have three tables: runs, users and a run_user pivot table. 我有三个表:运行,用户和run_user数据透视表。 I want to select all results from 'runs' and additional columns in the pivot table (which are 'completed', 'sticky', 'last_tested' and 'difficulty'), but only pull data from run_user for the current user. 我想从数据库表中的“运行”和其他列中选择所有结果(“已完成”,“粘性”,“最后一次”和“难度”),但仅从当前用户的run_user中提取数据。

In raw SQL I've managed to do this via a LEFT JOIN with an AND statement: 在原始SQL中,我设法通过带有AND语句的LEFT JOIN来完成此操作:

SELECT
runs.startpoint,
runs.startpostcode,
runs.endpoint,
runs.endpostcode,
run_user.completed,
run_user.sticky,
run_user.last_tested,
run_user.difficulty
FROM runs
LEFT JOIN run_user ON run_user.run_id=runs.id AND run_user.user_id = '2'

Any suggestions how to do this via the Query Builder? 有关如何通过查询生成器执行此操作的任何建议? I can do the LEFT JOIN in Laravel 4 but can't figure out how to combine this with an AND statement as well. 我可以在Laravel 4中进行LEFT JOIN,但也无法弄清楚如何将它与AND语句结合起来。

Any help is appreciated. 任何帮助表示赞赏。

Thanks! 谢谢!

As you requested, this is how you would do your query with the query builder: 根据您的要求,这是您使用查询构建器进行查询的方式:

DB::table('runs')
        ->leftJoin('run_user', function($join)
        {
            $join->on('run_user.run_id', '=', 'runs.id')
            ->on('run_user.user_id', '=', '2');
        })
        ->get();

But that JOIN statement looks a bit weird, you probably want to turn that into a normal WHERE (unless you have a very specific reason to filter directly in the JOIN clause). 但是这个JOIN语句看起来有点奇怪,你可能想把它变成一个普通的WHERE(除非你有一个非常具体的理由直接在JOIN子句中过滤)。

DB::table('runs')->join('run_user', 'run_user.run_id', '=', 'runs.id')->where('run_user.user_id', '=', '2')->get();

Docs: http://laravel.com/docs/queries#joins 文档: http//laravel.com/docs/queries#joins

In case anyone is wondering I got this working using DB::raw on one of the leftJoin parameters: 如果有人想知道我在其中一个leftJoin参数上使用DB :: raw工作:

DB::table('runs')
    ->leftjoin('run_user', 'runs.id','=', 
      DB::raw('run_user.run_id AND run_user.user_id = ' . $user->id))
    ->get();

It's not as 'eloquent' as I'd like it to be but without a working 'andOn' condition this seems to be the only way if you want to add an AND to the LEFT JOIN. 它并不像我希望的那样“雄辩”,但没有一个正常的'andOn'条件,如果你想在LEFT JOIN中添加一个AND,这似乎是唯一的方法。

If anyone can suggest a neater way I'd love to know! 如果有人能建议我更喜欢的整洁方式!

Thanks 谢谢

In Laravel 4 you can do this using Eloquent query. 在Laravel 4中,您可以使用Eloquent查询执行此操作。 I assume that you are aware of Eloquent relationships and know to to create models. 我假设您了解Eloquent关系并知道创建模型。 For your query it should be 对于您的查询,它应该是

RunUser::where('user_id',$id)->leftjoin('runs','runs.id','=','run_user.run_id')->first();

Note RunUser is the model for run_user table. 注意 RunUser是run_user表的模型。

use this 用这个

DB::table('runs')->select('runs.startpoint','runs.startpostcode','runs.endpoint','runs.endpostcode','run_user.completed','run_user.sticky','run_user.last_tested','run_user.difficulty')
    ->leftJoin('run_user', function($join)
    {
        $join->on('run_user.run_id', '=', 'runs.id')
        ->where('run_user.user_id', '2');
    })
    ->get();

Provident and user Table Join and get id to all details. Provident和用户表加入并获取所有详细信息的ID。

   $return = DB::table(config::get('databaseconstants.TBL_USER'))
            ->leftjoin('inex_pf_details', 'inex_users.id', '=', 'inex_pf_details.pf_user_id')
            ->select('inex_pf_details.*','inex_users.*')
            ->where('inex_pf_details.id', $id)
            ->first();
    return $return;

Here is must work: 这是必须的:

DB::table('runs')
        ->leftJoin('run_user', function($join) use ($user_id)
        {
            $join->on('run_user.run_id', '=', 'runs.id')
            ->where('run_user.user_id', '=', $user_id);
        })
        ->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM