SQL全文匹配值仅返回匹配项的布尔值(1或0)

时间:2012-07-18 14:17:57

标签: mysql match full-text-search

我有以下SQL语句:

SELECT
    g.*,
        (
            MATCH (g.GameTitle) AGAINST ('call of duty')
            OR g.GameTitle SOUNDS LIKE 'call of duty'
        )
    AS MatchValue,
    p.id AS platformid,
    p.alias AS PlatformAlias,
    p.name,
    p.icon
FROM
    games AS g,
    platforms AS p
WHERE
    (
        (
            MATCH (g.GameTitle) AGAINST ('call of duty')
            OR g.GameTitle SOUNDS LIKE 'call of duty'
        )
        OR
        (
            MATCH (g.Alternates) AGAINST ('call of duty')
            OR g.Alternates SOUNDS LIKE 'call of duty'
        )
    )

AND g.Platform = p.id
ORDER BY MatchValue DESC

在匹配FullText索引时返回正确的结果集,但报告的“MatchValue”仅具有布尔性质(0或1)。

如果我删除声明的第5行:

OR g.GameTitle SOUNDS LIKE 'call of duty'

我得到了不错的匹配值,从大约5.23到15.56,但在与“Alternates”匹配时我失去了一些功能。

我对SQL并不陌生,我花了几天时间让它运行起来......

有没有办法让第5行和第6行返回非布尔匹配值,以便我的结果正确排序?

提前致谢;)

1 个答案:

答案 0 :(得分:0)

当您说'ABC' OR (3 > 2)时,您要求BOOL结果。你不能得到字符串或布尔值。在上面的例子中,字符串'abc'转换为布尔 0 ,因为它不代表数字。

由于OR字符串和布尔值没有多大意义,我将两者分开:

SELECT
    g.*,
        (
            MATCH (g.GameTitle) AGAINST ('call of duty')
            OR g.GameTitle SOUNDS LIKE 'call of duty'
        )
    AS MatchResultBoolean,
    MATCH (g.GameTitle) AGAINST ('call of duty') AS MatchText,
    p.id AS platformid,
    p.alias AS PlatformAlias,
    p.name,
    p.icon
FROM
    games AS g,
    platforms AS p
WHERE
    (
        (
            MATCH (g.GameTitle) AGAINST ('call of duty')
            OR g.GameTitle SOUNDS LIKE 'call of duty'
        )
        OR
        (
            MATCH (g.Alternates) AGAINST ('call of duty')
            OR g.Alternates SOUNDS LIKE 'call of duty'
        )
    )

AND g.Platform = p.id
ORDER BY MatchValue DESC

请注意更改:

        (
            MATCH (g.GameTitle) AGAINST ('call of duty')
            OR g.GameTitle SOUNDS LIKE 'call of duty'
        )
    AS MatchResultBoolean,
    MATCH (g.GameTitle) AGAINST ('call of duty') AS MatchText,

这样你就可以得到两种方式:一次得到布尔值,在另一列中得到(可能)匹配。