简体   繁体   English

具有多个关键字的PHP / MySQL复杂搜索

[英]PHP/MySQL complex search with more than one keywords

I'm having the hardest time with a php (or mysql) search function. 我最难的是使用php(或mysql)搜索功能。 I'd be willing to buy a script for that, but i can't find any. 我愿意为此买一个剧本,但我找不到。

I have a customer table (firstname, lastname, street, zip, city etc....) and i would like to be able to not just look for one keyword but for 2 IN 2 DIFFERENT columns. 我有一个客户表(名字,姓氏,街道,邮编,城市等....)我希望能够不只是寻找一个关键字,但2 IN 2不同的列。

for instance: 例如:

Keyword: "John Doe" 关键词:“John Doe”

So my attempt was. 所以我的尝试是。

SELECT ....
   WHERE CONCAT(firstname,lastname) LIKE '%john%'
   AND CONCAT(firstname,lastname LIKE '%doe%'

However: that gives me back all johns and does and Mr. John Doe is somewhere in that list, but not on top, even though it's supposed to be the most relevant result. 然而:这让我回到了所有的johns并且确实和John Doe先生在这个列表中的某个地方,但不在顶部,尽管它应该是最相关的结果。

I also tried: 我也尝试过:

....
   WHERE MATCH(firstname,lastname) AGAINST('%john doe%')

And that pretty much gives back the same result. 这几乎可以给出相同的结果。

So the result I'm looking for would be: 所以我正在寻找的结果是:

1. John Doe (at first position!)
2. John Miller
3. John Smith
4. Harry Doe
5. Jack Doe
etc......

I've been looking for 2 hours and i refuse to believe I'm the first person who ever tried to do that :-) 我一直在寻找2个小时,我拒绝相信我是第一个试图这样做的人:-)

Any help is appreciated! 任何帮助表示赞赏!

Thanks! 谢谢!

Did you also try something like this 你也尝试过这样的事吗?

SELECT MATCH(firstname, lastname) AGAINST ('john doe') as Relevance 
FROM table WHERE MATCH(firstname, lastname) AGAINST('john doe' IN
BOOLEAN MODE) 
HAVING Relevance > 0.3
ORDER BY Relevance DESC

see also 也可以看看

http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html

This might be also a solution: 这可能也是一个解决方案:

SELECT MATCH(firstname) AGAINST ('john doe') as firstname_relevance, 
    MATCH(lastname) AGAINST ('john doe') as lastname_relevance
    FROM table WHERE MATCH(firstname, lastname) AGAINST('john doe' IN
    BOOLEAN MODE) 
    ORDER BY firstname_relevance+lastname_relevance DESC

Like this? 像这样?

SELECT
   firstname, lastname, othercol, MIN(Weighting)
FROM
    (
    SELECT firstname, lastname, othercol, 1 AS Weighting FROM...
    WHERE firstname = 'john' AND lastname  = 'doe'
    UNION ALL
    SELECT firstname, lastname, othercol, 2 AS Weighting FROM...
    WHERE firstname = 'john' OR lastname  = 'doe'
    ) T
GROUP BY
   firstname, lastname, othercol
ORDER BY
   MIN(Weighting) DESC;

Boolean mode do not automatically sort rows in order of decreasing relevance so you have to : 布尔模式不会按相关性递减的顺序自动对行进行排序,因此您必须:

SELECT MATCH(firstname,lastname) AGAINST('john doe') as Relevance FROM table WHERE MATCH
MATCH(firstname,lastname) AGAINST('john doe'  IN 
BOOLEAN MODE) ORDER 
BY Relevance DESC

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

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