简体   繁体   English

从PHP和MYSQL中的名字或姓氏的首字母搜索

[英]search from the first letter of first name or last name in PHP and MYSQL

I need to search first name or last name from the beginning of characters. 我需要从字符开头搜索名字或姓氏。 For example, firstname is Michael and lastname is Jordan. 例如,名字是Michael,姓氏是Jordan。 If I search "dan" or "hae" words, it shouldn't return result. 如果我搜索“ dan”或“ hae”单词,则不应返回结果。 Only if I search "mic" or "jor", it should return firstname and lastname together. 仅当我搜索“ mic”或“ jor”时,它才应一起返回名字和姓氏。 Is there any way to do it with mysql ? 有什么办法用mysql吗?

My following code does not return what I expect. 我的以下代码未返回我期望的结果。

select * from users where (firstname like '%$searchWord%' or lastname like '%$searchWord%')

如果只想从单词的开头搜索,则只需将通配符放在条件的末尾:

SELECT * FROM `users` WHERE (`firstname` LIKE '$searchWord%' OR `lastname` LIKE '$searchWord%')

Remove the first % in your query for each term. 在每个字词的查询中删除前一个%。

select * from users where (firstname like '$searchWord%' or lastname like '$searchWord%')

This will search for the first letters exactly with anything that matches following. 这将搜索与以下内容完全匹配的前几个字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM