如何在MySQL中搜索名字和姓氏?

时间:2016-12-21 15:20:51

标签: php mysql sql


我正在努力让用户能够从简单的数据库表中执行复杂的搜索。

表格

id, name
3,  Esteban Julio Ricardo Montoya De la Rosa Ramirez
4,  Romeo Heartless
5,  Rick Testing Astley
6,  Never Going to Give

当输入是“Esteban Montoya”,“Esteban la Rosa Remirez”,“Ramirez”等时,如何才能收到第3行?该应用程序是用PHP编程的。

4 个答案:

答案 0 :(得分:2)

一种方法是使用通配符:

where name like concat('%', replace($userinput, ' ', '%'), '%')

注意:这仍然要求名称的顺序正确(这是您的示例建议您所需的名称)。

答案 1 :(得分:1)

可以使用REGEXP 'Esteban Montoya|Esteban la Rosa Remirez|Ramirez'

答案 2 :(得分:1)

你可以使用这样的查询。它返回包含字段中两个单词的每一行。

SELECT *
FROM yourTable
WHERE `name` LIKE '%Esteban%' 
  AND `name` LIKE '%Montoya%';

答案 3 :(得分:1)

http://sqlfiddle.com/#!9/ad364/1

仍然需要搜索字词以正确的顺序显示,但似乎可以将template<typename T> concept bool myConcept() { return requires(T a) { { a.foo() } -> int; { a.bar(0) } -> int; }; } struct Object { int foo() {return 0;} int bar(int) {return 0;} }; static_assert(myConcept<Object>(), "Object does not adhere to myConcept"); 添加到开头,结束并替换所有空格:)

%
相关问题