开发者

How can I fetch "partial matches" with mysql?

I need to find best matches from a mysql table given a set of attributes.

For example, given ATTRIBUTE1, ATTRIBUTE2 and ATTRIBUTE3, I wa开发者_如何学Cnt to get the results as follows:

  1. rows with all attributes matched
  2. rows with 2 attributes matched
  3. rows with 1 attribute matched

so far I only know how to accomplish the first statement:

SELECT * FROM Users 
WHERE ATTRIBUTE1="aValue", ATTRIBUTE2="aValue", ATTRIBUTE3="aValue" 
LIMIT 20

N.B. I need 2 lists. A list with fully matching rows and a list with partial matches


you can consider to build an composite index in ATTRIBUTE{1..3}

this will benefits for List A

SELECT *
FROM Users 
WHERE ATTRIBUTE1="aValue" AND ATTRIBUTE2="aValue" AND ATTRIBUTE3="aValue"
LIMIT 20

and might help some row in List B

SELECT *, 
IF (ATTRIBUTE1="aValue", 1, 0) as a1, 
IF (ATTRIBUTE2="aValue", 1, 0) as a2, 
IF (ATTRIBUTE3="aValue", 1, 0) as a3
FROM Users 
WHERE ATTRIBUTE1="aValue" OR ATTRIBUTE2="aValue" OR ATTRIBUTE3="aValue" 
ORDER BY (a1+a2+a3) DESC
LIMIT 20
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜