2016-09-06 134 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)