如何连接2个表,以便可以使用过滤器查询两个表

时间:2019-12-24 07:25:47

标签: php mysql if-statement select where-clause

我有2个表,其中一个称为“广告”,另一个称为“用户”。我每个人都有不同的数据存储。 ads.ad_user列是使用users表中的users.user_id填充的。如何建立查询,以便可以从两个表中过滤数据。

这是我构建的查询,但不允许我独立地从两个表中过滤数据。

<div class="col-xs-6 col-sm-6">
    <div class="form-group">
    <label for="ethnicity">ETHNICITY</label>
    <select class="form-control ethnicitySelect" id="ethnicity" onchange="searchFilter();">
        <option selected disabled>Ethnicity...</option> 
        <option value="ethnicityall">All</option>
        <option value="other">Other</option>                                                
    </select>
</div>

<div class="form-group" style="padding:5px;color:#333;">
    <label for="citySelect">LOCATION</label>
    <select class="form-control citySelect" id="citySelect" onchange="searchFilter();">                                     
        <optgroup label="State">
        <option disabled selected>Select A City</option>
    </select>
</div>


$querySelectCount = "SELECT COUNT(ads.ad_id) as rowNum,
            ads.ad_id,
            ads.ad_title,
            ads.ad_content,
            ads.ad_date,
            ads.ad_user,
            ads.ad_photo,
            ads.ad_photo_thumb,
            ads.ad_age,
            ads.ad_city,
            ads.ad_rate,
            ads.ad_plan,
            ads.ad_approved,
            ads.adminPost,
            users.user_id,
            users.username,
            users.user_picture,
            users.userAge,
            users.height,
            users.hair,
            users.ethnicity,
            users.eyeColor,
            users.type              

            FROM
                ads
            LEFT JOIN
                users
            ON
                ads.ad_user = users.user_id                 

            $usersSQL

            WHERE 
                ads.ad_approved = '0'               
            $adsSQL $orderSQL                   
            ";

以下是我正在使用的几个过滤器。

if(isset($_POST['city']) && !empty($_POST['city']) && $_POST['city'] !== 'null'){   
    $city = $filter->filter($_POST['city']);
    $whereSQL .= " AND ads.ad_city = :city "; 
}else{
    $city = 0;
    $whereSQL .= " AND ads.ad_city >= :city "; 
}


if(isset($_POST['ethnicity']) && !empty($_POST['ethnicity']) && $_POST['ethnicity'] !== 'null'){
    $ethnicity = $filter->filter($_POST['ethnicity']);
    if($ethnicity == "ethnicityall"){
        $whereSQL .= "AND users.ethnicity != :ethnicity ";
    }else{
        $whereSQL .= "AND users.ethnicity = :ethnicity ";
    }
}else{
    $ethnicity = 0;
    $whereSQL .= "AND users.ethnicity >= :ethnicity ";
}

我正在使用多个下拉列表过滤数据,例如:当我选择一个城市时,直到我从用户表中选择种族,身高或眼睛颜色等选项,否则什么都不会显示...实际上,我需要选择以上所有内容甚至都不会显示。查询中的另一个SELECT可以帮助我实现我想要做的事情吗? 谢谢。

这也是我正在应用的过滤器     enter image description here

这是从ads表中选择一项并从users表中选择一项之后的查询图片     enter image description here

这是更新查询的打印输出

object(PDOStatement)#5(1){[“ queryString”] =>字符串(973)“ SELECT COUNT(ads.ad_id)as rowNum,ads.ad_id,ads.ad_title,ads.ad_content,ads.ad_date ,ads.ad_user,ads.ad_photo,ads.ad_photo_thumb,ads.ad_age,ads.ad_city,ads.ad_rate,ads.ad_plan,ads.ad_approved,ads.adminPost,users.user_id,users.username,users.user_picture,users .userAge,users.height,users.hair,users.ethnicity,users.eyeColor,users.type FROM ads LEFT JOIN users ads on ad.ad_user = users.user_id AND users.ethnicity!=:ethnicity AND users.eyeColor!=:eyeColor AND users.hair!=:hairColor AND users.height> = 0 位置 ads.ad_approved ='0'AND ads.ad_city =:city AND ads.ad_rate> = 0 AND ads.ad_content LIKE:关键字ORDER BY ad_date DESC“}

1 个答案:

答案 0 :(得分:1)

可能需要使用LEFT JOIN而不是INNER,然后可以将用户表的条件添加到ON语句中

LEFT JOIN users ON ads.ad_user = users.user_id AND users.ethnicity = :ethnicity
WHERE ads.ad_city = city

然后他们将独立工作