在 Metabase 中连接两个具有不同列名的 SQL 表
Joining two SQL tables with different column names in Metabase
我正在使用 Metabase 在我的数据库上提问。我正在尝试加入两个 tables,其中相同的信息(用户 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
我不断收到以下错误:
错误:对 table "game_states" 的 FROM 子句条目的引用无效 提示:也许您打算引用 table 别名 "gs"。位置:127
定义 table 别名后,使用它!
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
,因此可能想要包括所有匹配的游戏 -- 即使是那些没有匹配用户的游戏。如果是这样,请不要在 where
子句中过滤 u
并计算匹配项,这样您就可以得到 0
.
我正在使用 Metabase 在我的数据库上提问。我正在尝试加入两个 tables,其中相同的信息(用户 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
我不断收到以下错误: 错误:对 table "game_states" 的 FROM 子句条目的引用无效 提示:也许您打算引用 table 别名 "gs"。位置:127
定义 table 别名后,使用它!
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
,因此可能想要包括所有匹配的游戏 -- 即使是那些没有匹配用户的游戏。如果是这样,请不要在 where
子句中过滤 u
并计算匹配项,这样您就可以得到 0
.