douyou2234 2016-10-08 02:30
浏览 20
已采纳

从2个表中拉出以收集仅匹配项的结果

I have 2 tables in my SQL panel. I have one that appears like this

This one is called games:

ID | Game | Season
 1   MKX2     3
 2   GMR      4
 3   GH4      3

Then I have one for users signed up for a game called game_stats:

ID | User    | GID | Season | Points
 1   Jordan     1      2        10
 2   Jordan     1      3        15
 3   Jordan     3      3        25
 4   Grape      2      1        16
 5   Grape      2      4        17

How can I pull a user's most recent game_stats for the current season? So let's say I want to grab all of Jordan's most reason Game stats. However, I want to only grab the rows that match the game's season. So for example, If I grabbed Jordan I'd want these rows:

ID | User    | GID | Season | Points
 2   Jordan     1      3        15
 3   Jordan     3      3        25

So, ID 1 from game_stats is left out because there was no season matching the number "2" on the original games table. This would give me his current season if he is signed up for it.

How can I combine tables to do this?

  • 写回答

1条回答 默认 最新

  • dongxing4196 2016-10-08 02:38
    关注

    You can use INNER JOIN

    SELECT a.ID,
           a.USER,
           a.GID,
           a.Season,
           a.Points
    FROM   game_stats a
           JOIN games b
             ON a.GID = b.ID
                AND a.Season = b.m_season 
    

    or you can use Exists

    SELECT ID,
           USER,
           GID,
           Season,
           Points
    FROM   game_stats a
    WHERE  EXISTS (SELECT 1
                   FROM   game_stats b
                   WHERE  a.GID = b.ID
                          AND a.Season = b.m_season) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制