Php 使用concat选择firstname和lastname搜索
这是我的sql查询。我需要搜索名字和姓氏使用搜索Php 使用concat选择firstname和lastname搜索,php,mysql,Php,Mysql,这是我的sql查询。我需要搜索名字和姓氏使用搜索 $result = 'SELECT * FROM resume WHERE (first_name LIKE "'.$sKeyword.'%" OR last_name LIKE "'.$sKeyword.'%" OR CONCAT(first_name," ",last_name) like "'.$sKeyword.'%" O
$result = 'SELECT * FROM resume
WHERE (first_name LIKE "'.$sKeyword.'%" OR
last_name LIKE "'.$sKeyword.'%" OR
CONCAT(first_name," ",last_name) like "'.$sKeyword.'%" OR
email LIKE "'.$sKeyword.'%" OR
phone LIKE "'.$sKeyword.'%" OR
zip_code LIKE "'.$sKeyword.'%" OR
find_us LIKE "'.$sKeyword.'%" )';
在运行查询之前,只需检查表中是否存在数据 请尝试以下操作:
$fullName = "john martin";
$nameArray = explode(" ",$fullName);
if(count($nameArray)>1)
$search = " first_name in (".implode(',',$nameArray).") OR last_name in (".implode(',',$nameArray).")";
else
$search = " first_name LIKE "%'.$sKeyword.'%" OR last_name LIKE "%'.$sKeyword.'%" OR"
$seresult='SELECT * FROM resume
WHERE ('.$search.'
email LIKE "%'.$sKeyword.'%" OR
phone LIKE "%'.$sKeyword.'%" OR
zip_code LIKE "%'.$sKeyword.'%" OR
find_us LIKE "%'.$sKeyword.'%" )';
编辑:根据您的评论更新查询将结果提取为简单查询,并使用concatation显示结果
$seresult='SELECT * FROM resume
WHERE (first_name LIKE "%'.$sKeyword.'%" OR
last_name LIKE "%'.$sKeyword.'%" OR
email LIKE "%'.$sKeyword.'%" OR
phone LIKE "%'.$sKeyword.'%" OR
zip_code LIKE "%'.$sKeyword.'%" OR
find_us LIKE "%'.$sKeyword.'%" )';
在显示时和之后
<?= $seresult['first_name']; ?>-<?= $seresult['last_name']; ?>
-
试试这个
$seresult='SELECT * FROM resume
WHERE (first_name LIKE "%'.$sKeyword.'%" OR
first_name LIKE "'.$sKeyword.'%" OR
first_name LIKE "%'.$sKeyword.'" OR
last_name LIKE "%'.$sKeyword.'%" OR
last_name LIKE "'.$sKeyword.'%" OR
last_name LIKE "%'.$sKeyword.'" OR
email LIKE "%'.$sKeyword.'%" OR
phone LIKE "%'.$sKeyword.'%" OR
zip_code LIKE "%'.$sKeyword.'%" OR
find_us LIKE "%'.$sKeyword.'%" )';
将ORs拆分为不同的联合查询应该允许MySQL更有效地使用其索引 我假设您正在查找仅以输入的短语开头的项目。如果是这样的话,这可能会有所帮助,因为它只会在搜索短语中有空格的情况下对first_name和last_name进行组合搜索
$result = "SELECT * FROM resume
WHERE first_name LIKE '$sKeyword%'
UNION
SELECT * FROM resume
WHERE last_name LIKE '$sKeyword%'
UNION
SELECT * FROM resume
WHERE email LIKE '$sKeyword%'
UNION
SELECT * FROM resume
WHERE phone LIKE '$sKeyword%'
UNION
SELECT * FROM resume
WHERE zip_code LIKE '$sKeyword%'
UNION
SELECT * FROM resume
WHERE find_us LIKE '$sKeyword%'
UNION
SELECT * FROM resume
WHERE CONCAT(first_name,' ',last_name) like '$sKeyword%'";
$sKeywordArray = explode(' ', $sKeyword);
IF (count($sKeywordArray) > 1)
{
$result2 = array();
foreach($sKeywordArray $value)
{
$result2[] = "(first_name like '$value%' OR last_name like '$value%')";
}
$result .= " UNION SELECT * FROM resume WHERE ".implode(' AND ', $result2)
}
那么,您是否有任何错误?嗯,是否有任何错误或任何事情…因为我认为这将很好地工作..不,我没有从这个查询中得到任何错误,它只是在我使用firstname和last name搜索时找到空结果。这个查询无法正常工作。@$sKeyword适用于每个条件。奇怪的鱼!您的喜好没有前导%,因此他们只会在字段以您正在搜索的关键字开头的位置查找匹配项。添加前导%将阻止MySQL使用其索引,这将导致其性能非常差(即使您修复了此问题,大量OR子句也会导致索引问题)好的,谢谢suresh。希望这对meso raheel有效。您能告诉我正确的方法吗..实际上,当我在搜索中输入全名时,例如(john martin)它没有给我任何结果:(@Honey:than)你的查询和php代码将不同。用空格分解它,然后用in()在查询中搜索。结果会不同work@honey:更新了我的查询,请尝试。