开发者

Mysql-How to enforce the number of similar records retrieved?

I am working on a website that shows videos similar to that of youtube. This website has a section that shows the most ten similar videos used the FULL TEXT search to get the similar ones.

SELECT * , MATCH (`title` , `description`,`videokey` ) AGAINST ('$Keywords') AS score
FROM video
WHERE MATCH (`title` , `description`,`videokey` ) AGAINST ('$Keywords')
LIMIT 10

The problem is that sometimes it retrieves less than 10 videos. In this case, when there are less than 10 related videos, is there a way to enforce it to have 10 videos where there are not exactly related? If not, is it possible to have the 10 by another SELECT query based on a开发者_如何学编程nother filed such as category. How can I do this the best and quickest way possible?


Not really - your query needs to evaluate the conditional against all candidate rows before it has some idea of how many rows will be returned, hence the number of rows returned can't be used an an argument to a conditional in that query.

You could do two things (well, probably more, but here are two):

  • Simply perform a second query in your code, when the result rows are less than 10.
  • Grab another ten records in a second query that is appended to your first query via a UNION. http://dev.mysql.com/doc/refman/5.1/en/union.html In your code, just limit your iteration over results to a hard 10, rather than the result row size.

I'd say go with the first one (a second query). A second database query won't kill you, unless you're suffering from some massive read traffic, in which case better solutions involve not hitting the database at all if you can help it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜