【问题标题】:SQL group by, pass value to a join to get other dataSQL group by,将值传递给连接以获取其他数据
【发布时间】:2015-10-05 17:57:55
【问题描述】:

我正在尝试从garage_list:account_id 获取计数 从那里获取1个实例并获取相应的player_list:account_id 还有garage_list:tank_idtank_list:tank_id

结束表将如下所示:

tank :: count(玩家人数的替代文字)

IS-3 :: 2 -->(玩家 1,玩家 3)

E-100 :: 3 --> (player1, player 4, player 9)

Select Count(Distinct tank_id) As 
    counttanks , 
    Count(tank_id) As 
    counttanks ,
tank_id As tank_id From garage_list 



   RIGHT JOIN player_list 
        ON player_list.account_id = garage_list.account_id

   RIGHT JOIN tank_list 
        ON player_list.tank_id = tank_list.tank_id 

     Group By tank_id

  where tank_list.level='8',
  and player_list.clan='BAD-1'
  and player_list.account_id = '500549663'



  TABLES  
<garage_list>
account_id :: tank_id
1234       ::   20
1234       ::    44
4321       ::   18

    <tank_list>
    Tank_id :: name :: long_name :: row1 :: row2 :: row3 ::
    20     :: sherman::usa_sher

    <player_list>
    account_id :: clan :: nickname
    1234        ::bad-1:: joker
    4321       ::bad-g :: grumpy

【问题讨论】:

  • 你想要你的播放器列表是垂直的还是水平的?
  • 它将是垂直的,因为取决于级别的选项,我最终可能会得到 60 个奇怪的结果
  • 您的最终结果与提供的样本数据不符。

标签: sql right-join


【解决方案1】:

所以根据我的收集,你会想要这样的东西:

select pc.NumOfPlayers, tl.tank_id, pl.account_id, pl.clan, pl.nick
  from tank_list tl
  inner join garage_list gl
             on  tl.tank_id = gl.tank_id
  inner join player_list pl
             on gl.account_id = pl.account_id
  inner join (select tank_id, 
                     count(*) as NumOfPlayers 
                from garage_list group by tank_id) as pc
              on tl.tank_id = pc.tank_id
order by tl.tank_id

Check out SQL Fiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 2011-02-07
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2010-10-19
    相关资源
    最近更新 更多