我有一个项目,我写了一个搜索来查找联系人,一般来说有一个问题,搜索通过代码、电话、电子邮件引导。我可以编写一个 sql 函数来搜索单个请求,但是有没有办法确定在哪个参数上找到匹配项,通过电话、电子邮件或代码?刚才我提出了 3 个请求,tk。考虑找到匹配的参数,然后我需要输出,即 如果是电话,则输出电话,电子邮件表示电子邮件等。
搜索代码如下所示:
public function searchContactByPhoneCodeEmailOrderId($search) {
$search_condition = \cArray::stripQuotes([
'code' => \cString::prepareSearchString($search),
'email' => \cString::prepareSearchString($search),
'phone' => $search
]);
$type = 'code';
$response_contact = cFactory::getDB()->getRows("
SELECT TOP 15
ID_Code,
code,
ID_Contact
FROM
tblCode
WHERE
ID_Contact IS NOT NULL
AND code LIKE '" . $search_condition['code'] . "%'
", __FILE__, __LINE__);
if (empty($response_contact)) {
$response_contact = cAuthorizationModel::model()->getContactByOldCode($search_condition['code']);
}
if (empty($response_contact) && cChecker::phone($search_condition['phone'])) {
$type = 'phone';
$response_contact = cFactory::getDB()->getRows("
SELECT TOP 15
ID_Contact,
phone
FROM
tblContact
WHERE
phone LIKE '" . $search_condition['phone'] . "%'
", __FILE__, __LINE__);
}
...
}
嗯,我觉得本质很清楚,就不写太多了。
例如,在 SQL 语法中,这可能是这样的: