douren6874 2014-04-13 23:30
浏览 112
已采纳

MySQL根据2个ID查找ID

i would like to see if player A (9) and player B (14) have ever both entered the same round, one round has many entries by many players. this is the middle table of a many to many relationship between rounds and players

table: entries
id | roundID | PlayerID   
5  | 7       | 14
4  | 6       | 2
3  | 5       | 14
2  | 5       | 9
1  | 4       | 9

Im looking to return round ID 5 obviously, but what SQL statement does this need? a JOIN?

i could do it by getting all rounds played by player A and B seperately looping through As rounds and looping through Bs rounds on each iteration of A to look for a match, but that seems needlessly costly.

  • 写回答

3条回答 默认 最新

  • douyingbei1458 2014-04-14 02:12
    关注

    Something like this should work, basically getting a count of all the PlayerID enteries per roundID for only the specified players and restricting to show only ones with multiples.

    SELECT
        roundID
    FROM
        entries
    WHERE
        PlayerID IN (9, 14)
    GROUP BY
        roundID
    HAVING
        COUNT(*)>1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀