简体   繁体   中英

Join to Pivot Table Mysql

i am making a search with multiple inputs and consults to a database, my form is simple this is the structure of my 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

After i obtain and make the querys in this way

$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 ";

The Status and the Type has a foreign key in the properties table with status_id and type_id but the amenities is a pivot table properties_has_features i am trying to obtain that with the last two joins

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

But this not work, after all i fetch the arrays for get the values

$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."'
";

Whit this i get all the properties but not filtered for the amenities, if i remove the amenities of the query this work fine with type and status, i was looking and view some about Group_concat() and other things but i want suggest about my problem, thanks

UPDATE

This is the actual query what i get from that consult

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 '%'

UPDATE:

I need to obtain this

[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

This is pivot table properties_has_features

id | property_id | featured_id

The features table has

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM