1

我正在使用Metabase在我的数据库上提问。我正在尝试加入两个表,其中相同的信息(用户 ID)有两个不同的名称。我写的代码如下:

SELECT
    game_states.game_module AS game, count(*)
FROM
    game_states gs LEFT JOIN users u ON gs.user_id = u.id;
WHERE
    games_states.state = 'after_hands'
AND
    user.last_joined_stamp > now() - interval '30 days'
GROUP BY
    1
ORDER BY
    2 DESC

我不断收到以下错误: 错误:对表“game_states”的 FROM 子句条目的引用无效提示:也许您打算引用表别名“gs”。职位:127

4

1 回答 1

0

定义表别名后,使用它!

SELECT gs.game_module AS game, count(*)
-------^
FROM game_states gs LEFT JOIN
     users u
     ON gs.user_id = u.id;
WHERE gs.state = 'after_hands' AND
------^
      u.last_joined_stamp > now() - interval '30 days'
------^
GROUP BY 1
ORDER BY 2 DESC;

顺便说一句,您可能打算:

SELECT gs.game_module AS game, count(u.id)
FROM game_states gs LEFT JOIN
     users u
     ON gs.user_id = u.id AND
        u.last_joined_stamp > now() - interval '30 days'
WHERE gs.state = 'after_hands' 
GROUP BY 1
ORDER BY 2 DESC;

您正在使用LEFT JOIN,所以大概想要包括所有匹配的游戏——即使是那些没有匹配用户的游戏。如果是这样,请不要uwhere子句中过滤并计算匹配项,这样您就可以获得0.

于 2018-04-21T12:56:15.707 回答