我有一个搜索字词$term
。
我有3张桌子。
第一是vehicles
并且id
(int,autoincr。,unique),vehicle_id
(int) - 可以是相同的,vehicle_type
(int) - 可以是1或2 - 1表示汽车,2是摩托车。例如,它看起来如何:
----------------------------------
| id | vehicle_id | vehicle_type |
|--------------------------------|
| 1 | 546 | 1 |
----------------------------------
| 2 | 257 | 1 |
----------------------------------
| 3 | 546 | 2 |
----------------------------------
| 4 | 368 | 1 |
----------------------------------
第二是cars
并且id
(int,autoincr。,unique),title
- (varchar),示例:
-------------------
| id | title |
|------------------
|544 | Abbycar |
-------------------
|545 | Batmobil |
-------------------
|546 | WackyWheel |
-------------------
|547 | Ferrari |
-------------------
第3个是motorcycles
并且id
(int,autoincr。,unique),title
- (varchar),示例:
-------------------
| id | title |
|------------------
|544 | Abbycycle |
-------------------
|545 | Batcycle |
-------------------
|546 | WackyBike |
-------------------
|547 | Motorrari |
-------------------
现在,我想使用第一张表 vehicle_id (vehicle.vehicle_id)并将其加入第二张表 id (汽车) .id)但仅在 vehicle_type 为1(汽车)的条件下。与此同时,该车的标题应包含使用LIKE的 $ term 。
与此同时,我想加入第一张表 vehicle_id (vehicle.vehicle_id)和第三张表 id (摩托车。 id)但仅在 vehicle_type 为2(摩托车)的条件下。与此同时,摩托车的头衔应包含使用LIKE的 $ term 。
到目前为止,我有这段代码:
$result = DB::table('vehicles')->join('cars', function($join) use($term)
{
$join->on('vehicles.vehicle_id', '=', 'cars.id')
->where('vehicles.vehicle_type', '=', '1')
->where('cars.title', 'LIKE', '%' . $term . '%');
})
->join('motorcycles', function($join2) use($term)
{
$join2->on('vehicles.vehicle_id', '=', 'motorcycles.id')
->where('vehicles.vehicle_type', '=', '2')
->where('motorcycles.title', 'LIKE', '%' . $term . '%');
})
->get();
问题在于,当我有一辆同名的汽车和一辆摩托车时,其中包含部分汽车和摩托车,只有汽车被抓住。如果我评论汽车加入部分摩托车显示,但我想要两个。怎么做?
为什么忽略第二次加入?
因为这有效:
$result = DB::table('vehicles')->join('cars', function($join) use($term)
{
$join->on('vehicles.vehicle_id', '=', 'cars.id')
->where('vehicles.vehicle_type', '=', '1')
->where('cars.title', 'LIKE', '%' . $term . '%');
})
->get();
这也有效:
$result = DB::table('vehicles')->join('motorcycles', function($join2) use($term)
{
$join2->on('vehicles.vehicle_id', '=', 'motorcycles.id')
->where('vehicles.vehicle_type', '=', '2')
->where('motorcycles.title', 'LIKE', '%' . $term . '%');
})
->get();
这些查询中的每一个都给出了我预期的不同结果,但是,连续的2个连接对我没有任何帮助。
编辑:
结果:
$result = DB::table('vehicles')
->join('cars', 'vehicles.vehicle_id', '=', 'cars.id')
->join('motorcycles', 'vehicles.vehicle_id', '=', 'motorcycles.id')
->where('cars.title', 'LIKE', '%' . $term . '%')
->orWhere('motorcycles.title', 'LIKE', '%' . $term . '%')
->get();
就是这样:
[query] =>
select vehicles.vehicle_type as vehicle_type, vehicles.vehicle_id as vehicle_id
from `vehicles` inner join `cars` on `vehicles`.`vehicle_id` = `cars`.`id` and `vehicles`.`vehicle_type` = ?
inner join `motorcycles` on `vehicles`.`vehicle_id` = `motorcycles`.`id` and `vehicles`.`vehicle_type` = ? where `cars`.`title` LIKE ? or `motorcycles`.`title` LIKE ?
[bindings] => Array
(
[0] => 1
[1] => 2
[2] => %test%
[3] => %test%
)
答案 0 :(得分:0)
我没有对此进行测试,但这是一个可能的解决方案:
$result = DB::table('vehicles')
->join('cars', 'vehicles.vehicle_id', '=', 'cars.id')
->join('motorcycles', 'vehicles.vehicle_id', '=', 'motorcycles.id')
->where('cars.title', 'LIKE', '%' . $term . '%')
->orWhere('motorcycles.title', 'LIKE', '%' . $term . '%')
->get();
这应该导致sql语句如下:
select * from `vehicles`
inner join `cars` on `vehicles`.`vehicle_id` = `cars`.`id`
inner join `motorcycles` on `vehicles`.`vehicle_id` = `motorcycles`.`id`
where `cars`.`title` LIKE %term%
or `motorcycles`.`title` LIKE %term%
答案 1 :(得分:0)
试试这个:
$result = DB::table('vehicles')
->leftJoin('cars', function($join) {
$join->on('vehicles.vehicle_id', '=', 'cars.id')
->where('vehicles.vehicle_type', '=', 1);
})->leftJoin('motorcycles', function($join) {
$join->on('vehicles.vehicle_id', '=', 'motorcycles.id')
->where('vehicles.vehicle_type', '=', 2);
})
->where('cars.title', 'LIKE', '%' . $term . '%')
->orWhere('motorcycles.title', 'LIKE', '%' . $term . '%')
->get();