这有点简单,但不是同时。
我有一个用户输入名称的基本搜索框。
仅包含first_name
和last_name
的名称。
该表包含first_name
,last_name
,display_name
列,其中firs_name
和last_name
显然是名字和姓氏,display_name
是first_name
和last_name
的串联或首选名称,因此William Smith
的显示名称可能为Bill Smith
。
古怪:
名字和姓氏都可以是双姓。我的意思是我们可能有一个像
这样的名字First Name: Anna Maria Last Name: Smith
First Name: Anna Last Name: Bo Johnson
First Name: Anna Maria Last Name: Bo Johnson.
如此最佳情况,名称为Anna Smith
,但我已经有足够的最坏情况Anna Maria Bo Johnson
来完成此操作。
当用户搜索Anna Maria Bo Johnson
时,我需要运行一个不知道4"中的哪一个"是名字的第一个或最后一个名字,遗憾的是,双首名双姓有84个组合,如Anna Bo Maria Johnson
或Bo Maria Johnson Anna
等......
现在我的问题是,我不希望我的MySQL查询长度为84行:(3个字名示例)
SELECT * FROM tablename a
WHERE
(a.first_name = '$search_term[0]' AND a.last_name = '$search_term[1]') OR
(a.first_name = '$search_term[0]' AND a.last_name = '$search_term[2]') OR
(a.first_name = '$search_term[0]' AND a.last_name = '$search_term[1] $search_term[2]') OR
(a.first_name = '$search_term[0]' AND a.last_name = '$search_term[2] $search_term[1]') OR
(a.first_name = '$search_term[1]' AND a.last_name = '$search_term[0]') OR
(a.first_name = '$search_term[1]' AND a.last_name = '$search_term[1]') OR
(a.first_name = '$search_term[1]' AND a.last_name = '$search_term[0] $search_term[2]') OR
(a.first_name = '$search_term[1]' AND a.last_name = '$search_term[2] $search_term[0]') OR
(a.first_name = '$search_term[2]' AND a.last_name = '$search_term[0]') OR
(a.first_name = '$search_term[2]' AND a.last_name = '$search_term[1]') OR
(a.first_name = '$search_term[2]' AND a.last_name = '$search_term[0] $search_term[1]') OR
(a.first_name = '$search_term[2]' AND a.last_name = '$search_term[1] $search_term[0]') OR
(a.first_name = '$search_term[0] $search_term[1]' AND a.last_name = '$search_term[2]') OR
(a.first_name = '$search_term[1] $search_term[0]' AND a.last_name = '$search_term[2]') OR
(a.first_name = '$search_term[0] $search_term[2]' AND a.last_name = '$search_term[1]') OR
(a.first_name = '$search_term[2] $search_term[0]' AND a.last_name = '$search_term[1]') OR
(a.first_name = '$search_term[1] $search_term[2]' AND a.last_name = '$search_term[0]') OR
(a.first_name = '$search_term[2] $search_term[1]' AND a.last_name = '$search_term[0]') OR
a.display_name = '$search_term[0] $search_term[1] $search_term[2]' OR
a.display_name = '$search_term[1] $search_term[2] $search_term[0]' OR
a.display_name = '$search_term[2] $search_term[1] $search_term[0]'
ORDER BY last_name, first_name ASC
那么将这个查询写出来的快速而有效的方法是什么。基本上我需要将(=)每个单词与随机单个单词或2个单词组合的组中的所有字段进行比较。
提前感谢您的帮助!
答案 0 :(得分:1)
使用IN
声明:
SELECT *
FROM tablename a
WHERE concat_ws(' ',a.first_name,a.last_name) IN ('John Smith','Samantha Rose')
答案 1 :(得分:0)
我对你想要匹配的东西感到有点困惑。看看你的查询,看起来你和Maria Bo Johnson甚至Bo Anna都匹敌。那是为了吗?
如果是这样,你能做到吗
SELECT * FROM tablename a WHERE
(a.first_name = '$search_term[0]' OR a.first_name = '$search_term[1]' OR
a.first_name = '$search_term[2]' OR a.first_name = '$search_term[3]')
AND
(a.last_name = '$search_term[0]' OR a.last_name = '$search_term[1]' OR
a.last_name = '$search_term[2]' OR a.last_name = '$search_term[3]')
(可能添加了first_name!= last_name的检查...)
答案 2 :(得分:0)
不要爆炸,但要在mysql中连接
where concat(a.first_name, ' ', a.last_name) = '$search_terms' or a.display_name = '$search_terms'
如果输入可能只是名称的一部分,则使用LIKE:
where concat(a.first_name, ' ', a.last_name) LIKE '%$search_terms%' or a.display_name LIKE '%$search_terms%'
编辑如果您担心人们有时会以姓氏开头,有时会使用名字,再添加一个条件:
where concat(a.first_name, ' ', a.last_name) = '$search_terms' or concat(a.last_name, ' ', a.first_name) = '$search_terms' or a.display_name = '$search_terms'