将查询附加到MySQL全文查询

时间:2013-11-01 15:19:20

标签: mysql sql full-text-search rownum

我有这样的全文查询,它会得到我期望的结果

SELECT  id,'', 'blogs' as mytable,title, content, 
    MATCH(title) AGAINST('keywords*' IN BOOLEAN MODE) * 8 +
    MATCH(content) AGAINST('keywords*' IN BOOLEAN MODE) * 4
        as score FROM blogs WHERE archived = 'N' AND MATCH(title, content) 
            AGAINST('keywords*' IN BOOLEAN MODE)
             ORDER BY score DESC, id DESC

如果我搜索特定关键字,我会得到ID为4的结果。问题是我需要一个额外的列,以便我可以搜索该ID以获取该ID的行位置。

如果我自己运行此查询,我会得到正确的结果,即8

SELECT position FROM
(
   SELECT
   id, @rownum:=@rownum+1 position, archived
   FROM blogs, (SELECT @rownum:=0) r WHERE archived ='N'
   ORDER BY id DESC
)
AS position
WHERE id = 4

我想要做的是将这些查询合并到一个查询中,这样第一个查询的ID就会传递到第二个查询的WHERE中,所以我的附加列名为“position”,其值为8

我试图合并查询,但这绝对不对

1 个答案:

答案 0 :(得分:2)

这样的东西?

SELECT position FROM
(
   SELECT
   id, @rownum:=@rownum+1 position, archived
   FROM blogs, (SELECT @rownum:=0) r WHERE archived ='N'
   ORDER BY id DESC
)
AS position
WHERE id IN (SELECT id FROM(SELECT  id,'', 'blogs' as mytable,title, content, 
    MATCH(title) AGAINST('keywords*' IN BOOLEAN MODE) * 8 +
    MATCH(content) AGAINST('keywords*' IN BOOLEAN MODE) * 4
        as score FROM blogs WHERE archived = 'N' AND MATCH(title, content) 
            AGAINST('keywords*' IN BOOLEAN MODE)
             ORDER BY score DESC, id DESC)x)