使用 postgres 中主查询的值执行子查询
我试图通过在子查询中使用主查询中的相同值来匹配两个表之间的数据。为此,我使用了 3 个表:
世界 - 玩家所在的世界区域 玩家 - 实际玩家 被摧毁的物体 - 只是一个统计表,用于跟踪玩家在世界中的进度
我想做的是创建一个查询,显示游戏和他们所在的世界,并获取他们摧毁的所有物体的计数在那个世界里。我的查询目前是这样的:
select
world_id,
world_name,
player_id
count(select * from objects_destoryed where player_id = <insert player ID>) as Stats
from
Worlds
join Players using (player_id)
where
player_id = <insert player ID>
我要问的是是否有更好的方法而不是包含玩家 ID 两次来获取总体统计数据。还有一种方法可以扩展它以在那个世界的多个玩家上运行?例如在语句中使用player_id。
感谢您的任何建议
I am trying to match up data between two tables by using the same values from the main query in the sub-query. I am using 3 tables for this:
World - The area of the world that the player has been in
Player - The actual player
Objects destroyed - Just a stats table used to keep track of the player's progress in the world
What I am trying to do is to create a query that displays the play and the world they were in and get a count of all the objects they have destoryed in that world. My query is currently like this:
select
world_id,
world_name,
player_id
count(select * from objects_destoryed where player_id = <insert player ID>) as Stats
from
Worlds
join Players using (player_id)
where
player_id = <insert player ID>
What I am asking is if there is a better way rather then including the player ID twice to get the overall statistics. Also is there a way that I can expand this to run on multiple players that were in that world? Such as using a player_id in statement.
Thanks for any advice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有使用过 postgresql,但在其他风格的 SQL 中,类似这样的东西会起作用:
目标是 select 语句为世界上玩家销毁的每个对象返回一行,然后允许 group by 子句崩溃将具有相同世界和玩家的行合并为一行,并将行数合并到“统计数据”中。
为了让多个玩家返回结果,您只需更改 where 子句,使其与世界无关,而不是与玩家无关。每个存在于那个世界的玩家都会有自己的行。
I have not used postgresql, but in other flavors of SQL, something like this would work:
The goal is for the select statement to return one row for each object destroyed by the player in the world, and then allow the group by clause to collapse the rows with the same world and player into a single row, with the number of rows consolidated into "Stats."
In order to make this return results for several players, you would just change your where clause so that it keys off of the world, rather than the player. Each player with a presence in that world would have his own row.