2016-09-06 74 views
1

我有一个表,称为“庄园”,以及一些特点4分透视表一样的设施,intallations列表等从数据透视表视图的MySQL列添加

当(与Laravel 5个)我想进行搜索(庄园有大约200个田地和5个多重选择)我使用一个叫做“westates”的视图来加快搜索速度。但是当我必须搜索其他特性时,我使用了四个foreach(PHP),并且我跨越了所有结果并与数据透视表比较: - $ models是第一个查询的结果。

$extras = $request->ms_extras_amenities; //Items selected in multiselect 
    $others = $request->ms_more_features; 
    $facilities = $request->ms_other_installations; 
    $lapse = $request->lapse; 

    $models = $models->filter(function($model) use ($lapse,$extras,$others,$facilities) 
    { 
     $haslapse = false; 
     if(count($lapse)){ 
      foreach($lapse as $item){ 
       $result = \DB::table('rent_period_estates') 
        ->whereRaw("estates_id = $model->id AND rent_period_id = $item") 
        ->count(); 
       if($result != 0) $haslapse = true; 
      } 
     } 
     else 
      $haslapse = true; 

     $hasextras = true; 
     if(count($extras)){ 
      foreach($extras as $extra){ 
       $result = \DB::table('extras_amenities_estates') 
        ->whereRaw("estates_id = $model->id AND extras_id = $extra") 
        ->count(); 
       if($result == 0) $hasextras = false; 
      } 
     } 

     $hasothers = true; 
     if(count($others)) { 
      foreach ($others as $other) { 
       $result = \DB::table('other_chara_estates') 
        ->whereRaw("estates_id = $model->id AND other_chara_id = $other") 
        ->count(); 
       if ($result == 0) $hasothers = false; 
      } 
     } 
     $hasfacilities = true; 
     if(count($facilities)) { 
      foreach ($facilities as $facility) { 
       $result = \DB::table('other_facilities_estates') 
        ->whereRaw("estates_id = $model->id AND other_facilities_id = $facility") 
        ->count(); 
       if ($result == 0) $hasfacilities = false; 
      } 
     } 
    //Estate have to have all items selected in the multiselectd fields 
     if ($hasothers && $hasextras && $hasfacilities && $haslapse){ 
      return $model; 
     } 
    }); 

现在我有正确的结果,但与7个州测试我会做近300个查询。

我想知道如何创建视图,包括为了将透视条目逗号分隔为文本(这是一个想法),就像在数组中获取结果一样,按顺序对它进行分解([2,1,8,4 ]到“1,2,4,8”)在我看来,所以当我做一个查询时,我只能做一个查询添加: ... AND(westates.others = $ model-> others AND westates .extras = $ model-> extras AND ...)

可能是一个程序可以在MySQL服务器上执行它,但实际上我找不到解决方案。

这里的观点:

"CREATE OR REPLACE VIEW westates AS 
      SELECT 
       concat(estates.locale,estates.id) as code, 
       cities.name as city, 
       councils.name as council, 
       states.name as state, 
       countries.name as country, 
       concat(cities.name,' (',councils.name,')') as city_council, 
       estates.* 

      FROM estates 
       JOIN countries ON (estates.country_id = countries.id) 
       JOIN states  ON (estates.state_id = states.id) 
       JOIN councils ON (estates.city_council_id = councils.id) 
       JOIN cities  ON (estates.city_id = cities.id)" 

回答

0

感谢里克·詹姆斯(谁回答在另一篇文章)。对于任何有同样问题的人(对SQL很少经验),我想发布答案。 想象一下,我们必须使用表,Estates的主表以及一些名为estates_extras的额外数据透视表。

table estates 
columns id, address 

table estates_extras 
columns estate_id, extra_id 

我们希望有一个观点:

table westates 
columns estates.id, estates.address, extras 
like 
    id  address   extras 
    25000 My nice road 1,5,8 

首先: 如果我们不包括DISTINCT,我们将在我们认为有相同数量的重复列,作为元素在演员在这个例子中(含三级演员):

results 
    id  address   extras 
    25000 My nice road 1,5,8 
    25000 My nice road 1,5,8 
    25000 My nice road 1,5,8 

如果我们不加JOIN因为LEFT JOIN我们还没有结果置业不具有几乎是多余的。所以,在这个查询中,我们将拥有我们所有的遗产,如果他们有额外的,用逗号分隔的列表。

CREATE OR REPLACE VIEW westates AS 
    SELECT DISTINCT 
     estates.id, 
     estates.address, 
     (SELECT GROUP_CONCAT(estates_extras.extras_id) FROM estates_extras 
     WHERE estates_extras.estate_id = estates.id) AS extras 
    FROM estates 
    LEFT JOIN estates_extras ON (estates_extras.estates_id = estates.id)