我有5个字段名称,分别为first_name
,last_name
,middle_name
,status
和barangay_name
,我想根据用户想要搜索数据。仅最后一个字段名称为barangay_name
的问题正常。其余的不是,我的查询出了什么问题?我只检索活动的受益人,并且内部加入tbl_beneficiary
和tbl_barangay
。
public function searchActiveBeneficiary($keyword) {
$sql = "SELECT tbl_beneficiary.beneficiary_id,
tbl_beneficiary.first_name, tbl_beneficiary.last_name,
tbl_beneficiary.middle_name, tbl_beneficiary.is_active,
tbl_barangay.barangay_name FROM tbl_beneficiary INNER JOIN tbl_barangay
ON tbl_barangay.barangay_id = tbl_beneficiary.barangay_id WHERE
tbl_beneficiary.is_active = 'Yes' AND tbl_beneficiary.first_name OR
tbl_beneficiary.is_active = 'Yes' AND tbl_beneficiary.last_name OR
tbl_beneficiary.is_active = 'Yes' AND tbl_beneficiary.middle_name OR
tbl_beneficiary.is_active = 'Yes' AND tbl_barangay.barangay_name LIKE
:keyword";
$stmt = $this->connection->prepare($sql);
$stmt->execute([
":keyword" => '%' . $keyword . '%'
]);
return $stmt->fetchAll();
}
//vue.js method
searchActiveBeneficiary : function() {
axios({
method : "GET",
url : this.urlRoot + "beneficiary/search_active_beneficiary.php?keyword=",
params : {
keyword : this.search_active_beneficiary
}
}).then(function (response){
vm.active_beneficiaries = response.data;
console.log(response);
});
},
<?php
include '../../private/initialize.php';
$beneficiary = new Beneficiary();
echo json_encode($beneficiary->searchActiveBeneficiary($_GET['keyword']));
?>
答案 0 :(得分:1)
这是因为barangay_name
是您实际上与关键字进行比较的唯一对象。您需要将表达式的LIKE
部分添加到每个条件中。请注意,不必多次指定tbl_beneficiary.is_active = 'Yes'
,只需更改WHERE
条件的逻辑即可达到相同的效果:
WHERE tbl_beneficiary.is_active = 'Yes'
AND (tbl_beneficiary.first_name LIKE :keyword OR
tbl_beneficiary.last_name LIKE :keyword OR
tbl_beneficiary.middle_name LIKE :keyword OR
tbl_barangay.barangay_name LIKE :keyword)