Mysql在匹配布尔模式下选择next和previous ID

时间:2014-02-10 17:55:37

标签: mysql sql database

我正在尝试在Match boolean模式中查找下一个和上一个ID。这就是我的尝试:

SELECT id
FROM songs 
WHERE MATCH (artist) AGAINST ('alpha delta beta' IN BOOLEAN MODE)
ORDER BY MATCH (artist) AGAINST ('alpha delta beta' IN BOOLEAN MODE) DESC

结果:

32212
32138
32221
32235

SQL小提琴(结果):http://sqlfiddle.com/#!9/d369d/7

然后在id 32138之后找到下一个ID:

SELECT id
FROM songs 
WHERE 
    id < '32138'
  AND 
    MATCH (artist) AGAINST ('alpha delta beta' IN BOOLEAN MODE)
ORDER BY MATCH (artist) AGAINST ('alpha delta beta' IN BOOLEAN MODE) DESC LIMIT 1

根据上述结果,下一个id应为32221,但结果为空。

SQL小提琴(下一个ID):http://sqlfiddle.com/#!9/d369d/9

请帮忙吗?

1 个答案:

答案 0 :(得分:2)

您正在where子句中比较* id * s,并且您正在寻找低于最低id的{​​{1}}值。您想要与id结果进行比较:

match

编辑:

当然,多首歌曲可以具有相同的匹配值。考虑到这一点:

SELECT s.id
FROM songs s cross join
     (select MATCH (artist) AGAINST ('alpha delta beta' IN BOOLEAN MODE) as matchresult
      from songs s
      where id = 32138
     ) s32138
WHERE MATCH (artist) AGAINST ('alpha delta beta' IN BOOLEAN MODE) < s32138.matchresult and
      MATCH (artist) AGAINST ('alpha delta beta' IN BOOLEAN MODE)
ORDER BY MATCH (artist) AGAINST ('alpha delta beta' IN BOOLEAN MODE) DESC
LIMIT 1;