dqhr76378 2012-11-20 02:16
浏览 15
已采纳

使用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.

  • 写回答

2条回答 默认 最新

  • dongyun65343 2012-11-20 03:24
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题