[英]How can I join 2 tables so that I can use filters to query both
我有 2 个表,一个叫做“ads”,一个叫做“users”。 我在每个中存储了不同的数据。 ads.ad_user 列使用用户表中的 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 会帮助我实现我想要做什么吗? 谢谢。
这是从 ads 表中选择一个项目和从 users 表中选择一个项目后查询的图片
这是更新后的查询的打印输出
object(PDOStatement)#5 (1) { ["queryString"]=> string(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 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 :keywords ORDER BY ad_date DESC " }
可能您需要使用 LEFT JOIN 而不是 INNER,然后您可以将 users 表的条件添加到 ON 语句
LEFT JOIN users ON ads.ad_user = users.user_id AND users.ethnicity = :ethnicity
WHERE ads.ad_city = city
然后他们将独立工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.