我有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可以帮助我实现我想要做的事情吗?
谢谢。

这也是我正在应用的过滤器
    php - 如何联接2个表,以便可以使用过滤器查询两个表-LMLPHP

这是从ads表中选择一项并从users表中选择一项之后的查询图片
    php - 如何联接2个表,以便可以使用过滤器查询两个表-LMLPHP

这是更新查询的打印输出

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左联接用户ON ads.ad_user = users.user_id AND users.ethnicity!=:ethnicity AND users.eyeColor!=:eyeColor AND users .hair!=:hairColor AND users.height> = 0 WHERE ads.ad_approved ='0'AND ads.ad_city =:city AND ads.ad_rate> = 0 AND ads.ad_content LIKE:关键字按ad_date排序DESC“}

最佳答案

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

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


然后他们将独立工作

关于php - 如何联接2个表,以便可以使用过滤器查询两个表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59465309/

10-16 15:45