有没有一种方法,使这个查询更快:简化/加快SQL查询
$qur = mysql_query("
SELECT id, firstname, lastname,
(firstname = '$firstname' AND lastname = '$lastname') AS full FROM users
WHERE (firstname = '$firstname' AND lastname='$lastname')
OR (firstname LIKE '$firstname%' AND lastname LIKE '$lastname%')
OR firstname LIKE '$firstname%' OR lastname LIKE '$firstname%'
ORDER BY (firstname = '$firstname' AND lastname='$lastname') DESC");
我需要在这个什么:
Check if its a full match.
Get firstname lastname
Being able to only enter lastname "Fox" and let it find the firstname (get all users with lastname Fox, and show their firstname too and display check the script at bottom.)
Being able to only enter the firstname "Megan" and let it find the lastname (^)
Being able to only enter Megan F, and let it show "Megan Fox"
Being able to only enter Me Fox, and let it show "Megan Fox"
这是做什么的对我来说,工作无无问题。虽然也许我认为这会运行缓慢时,很多用户
我使用它这样运行它:
if (mysql_num_rows($qur) == 1) {
$get = mysql_fetch_array($qur);
if($get["full"] == 1){
echo $get["id"];
}else{
echo "Did you mean: ".$get["firstname"]." ".$get["lastname"]." ?";
}
}elseif(mysql_num_rows($qur) > 1){
while($get = mysql_fetch_array($qur)) {
$name[] = $get["firstname"]." ".$get["lastname"];
}
if(count($name) > 1) {
echo 'Who did you mean?<br>';
} else {
echo 'Did you mean: ';
}
echo implode('<br>', $name);
}
它不应该是“CONCAT(名字,‘’,姓)为满”而不是“(名字=‘$姓名’和姓氏='$ lastname')完整“? – 2010-11-05 21:37:16
@Paulo我不认为这就是他要做的。他想要一个完全匹配的布尔等值。 – cwallenpoole 2010-11-05 21:45:15
@Yes克里斯托弗 – Johnson 2010-11-05 21:46:04