MySQL搜索加入查询

时间:2011-09-24 01:49:28

标签: mysql sql database search-engine

我有一个多输入搜索表单,有两个文本框。 “searchWords”的一个文本框和“specificPeople”的另一个文本框。

在第一个文本框中,您可能拥有“Dorchester Hotel London”,而在第二个文本框中,您可能拥有“Brad Pitt / Angelina Jolie”。使用ASP,我将第二个文本框值转换为我的IN子句将接受的格式,例如('Brad Pitt','Angelina Jolie')。

SELECT photoSearch.photoID, Left(photoSearch.caption,25), photoSearch.allPeople, photoSearch.allKeywords
FROM photoSearch
JOIN ( photoPeople INNER JOIN people ON photoPeople.peopleID = people.peopleID)
ON photoSearch.photoID = photoPeople.photoID AND people.people IN ('Brad Pitt','Angelina Jolie')
WHERE MATCH (caption, allPeople, allKeywords) AGAINST ('+dorchester +hotel' IN BOOLEAN MODE)
AND
photoSearch.dateCreated BETWEEN '2011-07-21' AND '2011-10-23'
ORDER BY photoSearch.dateCreated

这没有错误,但它不会产生Brad和Angelina在一起的记录。它仅显示布拉德的记录和单独的安吉丽娜的记录。所以这就是我第一次意识到IN子句就像OR一样。

如何修改此查询,返回具有这两个特定名称的行,而不是其中任何一个?

我的数据库看起来与此相似:

photoSearch
photoID     INT / AUTO / INDEX
caption     VARCHAR(2500) /  FULLTEXT
allPeople   VARCHAR(300) / FULLTEXT
allKeywords VARCHAR(300) / FULLTEXT
dateCreated DATETIME / INDEX

photoPeople
photoID     INT / INDEX
peopleID    INT / INDEX

people
peopleID    INT / INDEX
people      VARCHAR(100) / INDEX

感谢任何帮助......一如既往:)


表格中的内容示例:

photoSearch
photoID     |       caption         |           dateCreated
1900                Dorchester Hotel...         2011-10-03

'photoPeople'
[photoID]       |   [peopleID]
1900                147
1900                148

'people'
[peopleID]      |   [people]
147                 Brad Pitt
148                 Angelina Jolie

2 个答案:

答案 0 :(得分:1)

加入photoPeoplepeople表n次,其中n是您要搜索的人数:

SELECT photoSearch.photoID, Left(photoSearch.caption,25), photoSearch.allPeople, photoSearch.allKeywords 
FROM photoSearch 
JOIN ( photoPeople AS pp1 JOIN people AS p1 ON pp1.peopleID = p1.peopleID) 
ON photoSearch.photoID = pp1.photoID AND p1.people = 'Brad Pitt'
JOIN ( photoPeople AS pp2 JOIN people AS p2 ON pp2.peopleID = p2.peopleID) 
ON photoSearch.photoID = pp2.photoID AND p2.people = 'Angelina Jolie'
WHERE MATCH (caption, allPeople, allKeywords)
      AGAINST ('+dorchester +hotel' IN BOOLEAN MODE) 
AND photoSearch.dateCreated BETWEEN '2011-07-21' AND '2011-10-23' 
ORDER BY photoSearch.dateCreated 

答案 1 :(得分:0)

问题在于IN子句

AND people.people IN ('Brad Pitt','Angelina Jolie')

查找完全匹配,因此查询正在完全按照您的要求进行。如果你做了

AND (people.people LIKE '%Brad Pitt%' OR people.people LIKE '%Angelina Jolie%') 

你会得到你想要的结果。如果您愿意,可以使用

AND instr('/Brad Pitt/Angel Jolie/','/'+people.people+'/') > 0

你可以得到你期望的结果..

试试这个:

SELECT photoSearch.photoID, Left(photoSearch.caption,25), photoSearch.allPeople,        photoSearch.allKeywords
    FROM photoSearch ps
    JOIN photoPeople pp on pp.photoId=ps.photoId
    JOIN people on people.peopleId = pp.PeopleId
    WHERE MATCH (caption, allPeople, allKeywords) AGAINST ('+dorchester +hotel' IN BOOLEAN MODE)
    AND photoSearch.dateCreated BETWEEN '2011-07-21' AND '2011-10-23'
    AND people.people IN ('Brad Pitt','Angelina Jolie')
    ORDER BY photoSearch.dateCreated