2017-07-17 78 views
1

如何过滤2个表中的数据集,其中第二个表的结果更多1:n。从2个表中筛选数据并从两个表中获取结果

在第一个表中我可以使用orWhere和我正在获取正确的数据,但我的另一个表包含多个结果,如果我使用包含或匹配我只从第二个表中获取数据。

所以,我想过滤两个表并获得匹配的数据。

这里是我的查询:

首先查询过滤第一个表

$query 
     ->where([ 
     'OR' => [ 
      'Orders.id' => $freeText, 
      'Orders.postal' => $freeText, 
      'Orders.address LIKE' => '%' . $freeText . '%', 
      'Orders.city LIKE' => '%' . $freeText . '%', 
      'Users.first_name' => $freeText, 
      'Users.last_name' => $freeText, 
      'ProjectAddresses.cost_centre' => $freeText, 
      'CONCAT(first_name, last_name) LIKE' => '%' . str_replace(' ', '', $freeText) . '%', 
      'CONCAT(first_name, last_name) LIKE' => '%' . $freeText . '%', 
      'Users.first_name IN ' => $splittedKeywords, 
      'Users.last_name IN ' => $splittedKeywords, 
     ] 
     ]); 

第二次查询 - 尝试从第二表过滤数据(但仍需从第一个表匹配的数据)

$query->contain('Items', function ($q) use ($freeText) { 
    return $q->where(['vessel_id' => $freeText]); 
}); 

所以问题是如果我使用第二个查询,他会自动从第二个表中获取数据,我的目标是获取所有过滤的数据(来自第一个和第二个表)。

我有20+的数据集,如:

(int) 0 => [ 
     'id' => (int) 1, 
     'uuid' => '5f34ecda-6bc6-46ed-b5cc-b2227029aed8', 
     'user_id' => (int) 319, 
     'status' => (int) 30, 
     'order_price' => (float) 341.04, 
     'address_id' => (int) 379, 
     'address' => 'XYZ', 
     'building_number' => '171', 
     'postal' => '111', 
     'city' => 'XYZ', 
     'country' => 'AT', 
     'project_address' => [ 
      'id' => (int) 379, 
      'type' => 'project', 
      'group_id' => (int) 3, 
      'default' => false, 
      'corresponding_invoice_address' => null, 
      'short_name' => 'XYT', 
      'comment' => '', 
     ], 
     'user' => [ 
      'id' => (int) 319, 
      'uuid' => '675216eb-7110-44d2-82a7-f7f020e934a6', 
      'title' => 'Herr', 
      'first_name' => 'Test', 
      'last_name' => 'Test', 
     ], 
     'item_groups' => [], 
     'items' => [ 
      (int) 0 => [ 
       'id' => (int) 26, 
       'uuid' => 'f4f629be-e25e-4432-8d97-6b2adcee9065', 
       'item_group_id' => null, 
       'type' => (int) 2, 
       'status' => (int) 30, 
       'vessel_id' => (int) 40001, 
       'features' => [], 
      ], 
      (int) 1 => [ 
       'id' => (int) 28, 
       'uuid' => 'f4f629be-e25e-4432-8d97-6b2adcee9065', 
       'item_group_id' => null, 
       'type' => (int) 2, 
       'status' => (int) 30, 
       'vessel_id' => (int) 40003, 
       'features' => [], 
      ], 
      (int) 1 => [ 
       'id' => (int) 29, 
       'uuid' => 'f4f629be-e25e-4432-8d97-6b2adcee9065', 
       'item_group_id' => null, 
       'type' => (int) 2, 
       'status' => (int) 30, 
       'vessel_id' => (int) 40003, 
       'features' => [], 
      ], 
     ] 
    ], 

SQL

SELECT * 
FROM orders Orders 
INNER JOIN users Users ON Users.id = (Orders.user_id) 
LEFT JOIN addresses ProjectAddresses ON ProjectAddresses.id = (Orders.address_id) 
WHERE (Orders.id = :c0 
     OR Orders.postal = :c1 
     OR Orders.address LIKE :c2 
     OR Orders.city LIKE :c3 
     OR Users.first_name = :c4 
     OR Users.last_name = :c5 
     OR ProjectAddresses.cost_centre = :c6 
     OR CONCAT(first_name, last_name) LIKE :c7 
     OR Users.first_name IN (:c8) 
     OR Users.last_name IN (:c9)) 

参数是c1 = 4001 || %40001% || %40001

@ndm的目标是如果有人在$freeText == 40003送我得为导致这个对象vessel_id = 40003和多数民众赞成在工作,但如果有人发送$freeText == Test那么我需要再次相同的结果,因为first_name == Test 8see第一个查询),当在第二wuery我使用匹配/包含此结果被删除,因为他只“取”行匹配/包含项目...

基本上我想检查10 +列与给定$ freeText变量,如果它匹配我希望结果在我的数据集(来自两个表)

+0

“_So问题是,如果我使用第二个查询,他会自动从第二table_只取数据” 与您所描述的关系,我真的不明白这是怎么发生的。包含的关联只能在使用INNER连接的“1:1”或“n:1”关系的情况下减少主查询的结果。请显示一些(调试)结果,如生成的SQL,检索到的数据与预期的数据,... – ndm

+0

@ndm我已更新我的问题... – JohnWayne

+0

我还是不太明白。你似乎描述了你展现的完全相反。当查询'Test'而不是'40003'时,我可以看到'Items'如何可能会丢失,并且我可以看到在查询'40003'而不是'Test'时如何丢失命令,但是反过来却没有。匹配是一个不同的故事,但是你在这里显示包含,并且根据显示的SQL,主查询不受此影响。 – ndm

回答

2

如果我正确理解你,那么你正在寻找一个左连接过滤器,即左加入Items(另外包含它检索),Orders主键(避免重复),然后只需将Items.vessel_id条件添加到主要查询WHERE子句,以使其成为OR条件。

$query 
    ->contain('Items') 
    ->leftJoinWith('Items') 
    ->where([ 
     'OR' => [ 
      'Orders.id' => $freeText, 
      // ... 
      'Items.vessel_id' => $freeText 
     ] 
    ]) 
    ->groupBy('Orders.id'); 

又见

+0

无处不在的地方发送$ var和filter结果这是我的问题的解决方案:)谢谢! – FreeMooonSpider