簡體   English   中英

聯接到數據透視表Mysql

[英]Join to Pivot Table Mysql

我正在用多個輸入進行搜索並查詢數據庫,我的表單很簡單,這就是我的POST的結構

[search-word] => casas
[status] => sale,rent
[type] => condo,house,commercial,lot,fractional,business,boat,villa
[beds] => 2
[baths] => 3
[amenities] => handicap facilities,children allowed,security,furnished,pet friendly,jacuzzy

在我以這種方式獲得並進行查詢之后

$query = "SELECT pr.* FROM properties as pr 
  INNER JOIN status as st
    ON pr.status_id = st.id
  INNER JOIN types as ty
    ON pr.type_id = ty.id
  INNER JOIN property_has_features as prfe
    ON prfe.property_id = pr.id
  INNER JOIN features as fe
    ON prfe.feature_id = fe.id
  WHERE ";

狀態和類型在具有status_id和type_id的屬性表中具有外鍵,但是便利設施是數據透視表properties_has_features我試圖通過最后兩個聯接來獲取

INNER JOIN property_has_features as prfe
ON prfe.property_id = pr.id
INNER JOIN features as fe
ON prfe.feature_id = fe.id

但這不起作用,畢竟我獲取了數組以獲取值

$status = explode(",", $status);
$type = explode(",", $type);
$amenities = explode(",", $amenities);
$statusCount = 0;
$typeCount = 0;
$amenitiesCount = 0;

foreach ($status as $v) {
  if ($statusCount > 0){
    $query .= "OR ";
  }
  $query .= "st.title = '$v' "; 
  ++$statusCount;
}

foreach ($type as $k) {
  if ($typeCount > 0){
    $query .= "OR ";
  }else{
    $query .= "AND ";
  }
  $query .= "ty.title = '$k' "; 
  ++$typeCount;
}

foreach ($amenities as $j) {
  if ($amenitiesCount > 0){
    $query .= "OR ";
  }else{
    $query .= "AND ";
  }
  $query .= "fe.title = '$j' "; 
  ++$amenitiesCount;
}

$query .= "
  AND pr.beds LIKE '%".$beds."' 
  AND pr.baths LIKE '%".$baths."'
";

惠特這我獲得了所有屬性,但未篩選便利設施,如果我刪除查詢的便利設施,此類型和狀態都很好,我一直在尋找並查看一些有關Group_concat()的信息以及其他信息,但是我想建議我的問題, 謝謝

UPDATE

這是我從該咨詢中得到的實際查詢

SELECT pr.* FROM properties as pr 
  INNER JOIN status as st
    ON pr.status_id = st.id
  INNER JOIN types as ty
    ON pr.type_id = ty.id
  INNER JOIN property_has_features as prfe
    ON prfe.property_id = pr.id
  INNER JOIN features as fe
    ON prfe.feature_id = fe.id
  WHERE st.title = 'sale' OR st.title = 'rent' AND ty.title = 'condo' OR ty.title = 'house' OR ty.title = 'commercial' OR ty.title = 'lot' OR ty.title = 'fractional' OR ty.title = 'business' OR ty.title = 'boat' OR ty.title = 'villa' AND fe.title = 'handicap facilities' OR fe.title = 'children allowed' OR fe.title = 'security' OR fe.title = 'furnished' OR fe.title = 'pet friendly' OR fe.title = 'jacuzzy' 
AND pr.beds LIKE '%' 
AND pr.baths LIKE '%'

更新:

我需要獲得這個

[id] => 172
[permalink] => https://sample.com/properties-for-sale/villa-1-tres-mares
[title] => Villa 1 Tres Mares
[description] => Beautiful and spacious oceanfront villa at the exclusive Tres Mares Development. This fully furnished 5 bedroom and 7 bathroom unit is ideal for families and the large terraces and balconies provide outstanding views of the bay, yachts and cruises. Its’ unique location at the Tres Mares Development allows access to upscale amenities such as a private beach, infinity pool, spa and health club, tennis courts, concierge service and poolside restaurant. Ready to move in, Villa 1 Tres Mares is the ultimate modern and luxurious Marina living experience.
[images] => {"image0":"https:\/\/sample.com\/wp-content\/uploads\/2018\/01\/whatsapp-image-2018-01-17-at-13-58-37-1-250x120.jpeg","image1":"https:\/\/sample.com\/wp-content\/uploads\/2018\/01\/whatsapp-image-2018-01-17-at-13-58-29-11-250x120.jpeg","image2":"https:\/\/sample.com\/wp-content\/uploads\/2018\/01\/whatsapp-image-2018-01-17-at-13-58-26-1-250x120.jpeg"}
[position] => {"lat":"20.6565743795159","lng":"-105.24673819541931"}
[price] => 1750000
[baths] => 7
[beds] => 5
[parking] => 2
[lang] => en
[property_id] => 6640
[status_id] => 1
[location_id] => 11
[type_id] => 11
[created_at] => 2018-01-24 18:08:58
[updated_at] => 2018-01-24 18:08:58

這是數據透視表的properties_has_features

id | property_id | featured_id

功能表具有

id | title 

在此處輸入圖片說明

// I'm largely leaving your code as-is, but you could replace the
// list of ORs with an IN()
// The main change is that you need to enclose each attribute group
// within parenthesis so that the logical AND/OR processing is correct
if(is_array($status) && !empty($status))
{
    foreach ($status as $v) {
        if ($statusCount > 0){
            $query .= "OR ";
        }
        else{
            $query .= "(";
        }
        $query .= "st.title = '$v' "; 
        ++$statusCount;
    }

    $query .= ")";
}

if(is_array($type) && !empty($type))
{
    foreach ($type as $k) {
        if ($typeCount > 0){
            $query .= "OR ";
        }else{
            $query .= "AND (";
        }
        $query .= "ty.title = '$k' "; 
        ++$typeCount;
    }

    $query .= ")";
}

if(is_array($amenities) && !empty($amenities))
{
    foreach ($amenities as $j) {
        if ($amenitiesCount > 0){
            $query .= "OR ";
        }else{
            $query .= "AND (";
        }
        $query .= "fe.title = '$j' "; 
        ++$amenitiesCount;
    }

    $query .= ")";
}

// I'm not sure about the logic here, but I don't know what the data
// in your pr.beds and pr.baths columns looks like. I would think
// you'd want some sort of greater-than-equal comparison here or something
$query .= "
  AND pr.beds LIKE '%".$beds."' 
  AND pr.baths LIKE '%".$baths."'
";

DEMO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM