我有一個表,稱爲「莊園」,以及一些特點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)"