加入Pivot Table Mysql

时间:2018-01-31 17:38:21

标签: php 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()和其他的东西,但我想建议关于我的问题,谢谢

更新

这是我从该咨询中获得的实际查询

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 

enter image description here

1 个答案:

答案 0 :(得分:1)

// 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

相关问题