English 中文(简体)
PHP/MySQL综合搜索,有不止一个关键词
原标题:PHP/MySQL complex search with more than one keywords
  • 时间:2012-01-13 10:57:21
  •  标签:
  • php
  • mysql

I m having the hardest time with a php (or mysql) search function. 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.

例如:

关键词:“John Doe”

因此,我的尝试是。

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

然而,这给我留下了所有的 j子,John Doe先生是名单上的某个地方,但不是站在上,尽管它本应是最重要的结果。

I also tried:

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

而这种结果恰恰相反。

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

我looking了两小时,我拒绝相信我是最早试图这样做的人:

任何帮助都值得赞赏。

Thanks!

问题回答

你们也试图这样做。

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

另见《经济及社会理事会正式记录,2008年,补编第6号》(E/2008/44)。

rel=“nofollow”http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html

这可能也是解决办法:

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;

诱杀模式不会自动地划分,从而降低相关性,因此,你必须:

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




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签