相关疑难解决方法(0)

SQL join:where子句与on子句

阅读之后,这不是Explicit vs Implicit SQL Joins的重复.答案可能是相关的(甚至是相同的),但问题是不同的.


有什么区别,应该分别做些什么?

如果我理解正确的理论,查询优化器应该能够互换使用.

sql join on-clause where-clause

616
推荐指数
10
解决办法
62万
查看次数

使用查询生成器或Eloquent加入附加条件

我正在尝试使用Laravel查询生成器的JOIN查询添加条件.

<?php

$results = DB::select('
       SELECT DISTINCT 
          *
          FROM 
             rooms 
                LEFT JOIN bookings  
                   ON rooms.id = bookings.room_type_id
                  AND (  bookings.arrival between ? and ?
                      OR bookings.departure between ? and ? )
          WHERE
                bookings.room_type_id IS NULL
          LIMIT 20',
    array('2012-05-01', '2012-05-10', '2012-05-01', '2012-05-10')
);
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用Raw表达式,但之后会有SQL注入点.我已经尝试了以下查询生成器,但生成的查询(显然,查询结果)不是我想要的:

$results = DB::table('rooms')
    ->distinct()
    ->leftJoin('bookings', function ($join) {
        $join->on('rooms.id', '=', 'bookings.room_type_id');
    })
    ->whereBetween('arrival', array('2012-05-01', '2012-05-10'))
    ->whereBetween('departure', array('2012-05-01', '2012-05-10'))
    ->where('bookings.room_type_id', '=', null)
    ->get();
Run Code Online (Sandbox Code Playgroud)

这是Laravel生成的查询:

select distinct * from `room_type_info`
    left join `bookings` 
on `room_type_info`.`id` = `bookings`.`room_type_id` …
Run Code Online (Sandbox Code Playgroud)

laravel laravel-4 laravel-query-builder

72
推荐指数
6
解决办法
12万
查看次数