选择其中一个OneToMany,其中One没有一定的结果

时间:2016-06-30 21:49:18

标签: php mysql sql laravel laravel-4

我的WorkOrder有很多LineItem个。我只想选择具有特定WorkOrder但没有其他LineItem的{​​{1}}。具体示例是选择所有LineItem用于购买新轮胎的WorkOrder,而不是购买新的拖车轮胎。

表格数据

LineItem

期望的结果:

WorkOrder
id  create_date
1   2015-01-01 12:00:00
2   2015-01-01 12:00:00

LineItems
id  order_id    line_type
1   1           tire disposal
2   1           new trailer tires
3   1           tire pro-tection plan
4   2           tire disposal
5   2           new car tires
6   2           tire pro-tection plan

当前代码:

WorkOrder
id  create_date
2   2015-01-01 12:00:00

$search = [ 'disposal', 'rotation', 'pro-tection', ]; $selects = [ 'Customer.*', 'Vehicle.*', 'WorkOrder.*', 'max(WorkOrder.create_date) as create_date', ]; $orders = $this->prospectInstance->getDatabase()->table('WorkOrder') ->join('LineItems', 'LineItems.order_id', '=', 'WorkOrder.id') ->join('Customer', 'Customer.customer_id', '=', 'WorkOrder.customer_id') ->join('Vehicle', 'Vehicle.customer_id', '=', 'Customer.customer_id') ->selectRaw(implode(',', $selects)) ->whereNotNull('Customer.email') // this does nothing ->where('LineItems.line_type', 'not like', '%trailer%') ->where(function($query) use ($search) { foreach ($search as $term) { $query->orWhere('LineItems.line_type', 'like', "%$term%"); } }) ->groupBy('WorkOrder.customer_id') ->get(); 生成的SQL查询是:

$orders

目前,检查select Customer.*, Vehicle.*, WorkOrder.*, max(WorkOrder.create_date) as create_date from `WorkOrder` inner join `LineItems` on `LineItems`.`order_id` = `WorkOrder`.`id` inner join `Customer` on `Customer`.`customer_id` = `WorkOrder`.`customer_id` inner join `Vehicle` on `Vehicle`.`customer_id` = `Customer`.`customer_id` where `Customer`.`email` is not null and `LineItems`.`line_type` not like ? and ( `LineItems`.`line_type` like ? or `LineItems`.`line_type` like ? or `LineItems`.`line_type` like ? ) group by `WorkOrder`.`customer_id` 是否适用于新的拖车轮胎的当前状况不起作用。它只是忽略了行而仍然给出了结果LineItem,因为它匹配所有其他术语。

如果没有包含{{1}的WorkOrderWorkOrder,我如何选择包含LineItem s的LineItem个指定字词 }}?

备注:

  • 我不直接选择线型trailer的位置的原因是因为它们遍布整个地方。选择新轮胎工单的唯一可靠方法是检查轮胎处理费或轮胎保修是否有订单项。

0 个答案:

没有答案