使用mysql顶部的“like”显示查询的搜索记录
I am writing a search query which makes a search based on first name and last name. The query is like this:
select fname,lname
from users
where fname like fname like '%$name%' or lname like '%$name%';
if the user enters first name and last name in the search box,I have exploded it with a 'space' and the query goes like this
select fname,lname
from users
where fname like fname like '%$f_name%' or lname like '%$l_name%';
In the second query if I enter John Thomas,it shows me all the records with John or Thomas in first or last name,but the actual search result i.e John Thomas(exact match) is somewhere below in the results.(If I have 100 results its on the 60th position).
How can I modify the query to achive this or do I have to handle it programatically? i.e Check the result array and match the check box value for its presence and display it fist.
我正在编写一个搜索查询,根据名字和姓氏进行搜索。 查询是这样的 : p>
选择fname,lname
from users
其他fname,如fname,如'%$ name%'或lname,如'%$ name%';
code> pre>
如果用户在搜索框中输入名字和姓氏,我用'空格'展开它,查询就像这样 p>
选择fname,lname
from users
其他fname,如fname,如'%$ f_name%'或lname,如'%$ l_name%';
code> pre>
在第二个查询中,如果我输入 John Thomas b>,它会显示John或Thomas姓氏或姓氏的所有记录,但实际搜索结果即John Thomas(完全匹配 strong >)在结果的下方。(如果我有100个结果它在第60个位置)。
p>
我如何修改查询以实现这个或我必须 以编程方式处理它? 即检查结果数组并匹配其存在的复选框值并显示它。 p>
div>
Try:
SELECT fname, lname FROM users
WHERE (fname = '$f_name' AND lname = '$l_name')
OR (fname LIKE '%$f_name%') OR (lname LIKE '%$l_name%');
And if you need to order then ORDER BY lname, fname
in the end of the query, depending on what you need to order by of course.
ADD in query
ORDER BY `fname`
You can testing mysql sorting More information here ==> http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html