where子句中的未知列,它确实存在

时间:2013-11-26 19:11:30

标签: mysql sql

我有以下查询:

SELECT
    *
FROM
    levelsloaded,
    (
        SELECT
            *
        FROM (
            (
                SELECT
                    id,
                    globalId,
                    date,
                    serverId,
                    playerId,
                    'playerjoins' AS origin 
                FROM
                    playerjoins
                WHERE
                    playerId = 2224
                    AND date <= levelsloaded.date 
                ORDER BY
                    date DESC
            ) UNION ALL (
                SELECT
                    id,
                    globalId,
                    date,
                    serverId,
                    playerId,
                    'playerleaves' AS origin 
                FROM
                    playerleaves
                WHERE
                    playerId = 2224
                    AND date <= levelsloaded.date 
                ORDER BY
                    date DESC
            )
            ORDER BY
                date DESC
            LIMIT 1
        ) below2
    ) below

我可以向你保证levelsloaded.date确实存在。

为什么我一直收到错误:SQL Error (1054): Unknown column 'levelsloaded.date' in 'where clause'。 我认为SELECT * FROM levelsloaded已经从levelsloaded选择了所有内容。

2 个答案:

答案 0 :(得分:1)

问题是您的联接不知道使用levelsloaded.date的上下文。

<强>更新

我没有对此进行测试,但它应该让你非常接近。一旦我们得到蛮力解决方案,我认为我们可以提高效率。

选择         SERVERID,         MAX(date)AS level_date     从         水平负载AS ll     通过...分组         SERVERID

SELECT
    ll.*,
    pj.*,
    pl.*
FROM
    levelsloaded AS ll
JOIN (
    SELECT
        server_id,
        playerId,
        MAX(`date`) AS join_date
    FROM
        playerjoins
    WHERE
        playerId = 2246
    GROUP BY
        server_id,
        playerId
) AS pj ON pj.server_id = ll.server_id
JOIN (
    SELECT
        server_id,
        playerId,
        MAX(`date`) AS leave_date
    FROM
        playerleaves
    WHERE
        playerId = 2246
    GROUP BY
        server_id,
        playerId
) AS pl ON pl.server_id = ll.server_id
WHERE
    ll.date BETWEEN pj.join_date AND pl.leave_date;

答案 1 :(得分:0)

在列名称周围使用后退标记。您有一些列名也是关键词。解析器可能会感到困惑。