0

我有以下 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

它在与全文索引匹配时返回一组正确的结果,但是报告的“MatchValue”仅具有布尔性质(0 或 1)。

如果我删除语句的第 5 行:

OR g.GameTitle SOUNDS LIKE 'call of duty'

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

我对 SQL 并不精通,我花了好几天的时间才让它运行得这么好......

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

提前致谢 ;)

4

1 回答 1

0

When you say 'ABC' OR (3 > 2) you are asking for a BOOL result. You cannot either get a string or a boolean. In the above case, the string 'abc' translates to boolean 0, since it does not represent a number.

Since it does not make much sense in ORing a string and a boolean, I would separate the two:

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

Note the change:

        (
            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,

This way you get it both ways: one time you get the boolean value, in a nother column you get the (possible) match.

于 2012-07-18T14:45:39.447 回答