简体   繁体   English

如何使用MySQL选择子集

[英]How to select a subset using MySQL

​ In general, I'm trying to learn the best MySQL practice for selecting a set of records in one table whose fields are a subset of records from another table. 一般来说,我正在尝试学习最佳的MySQL实践,在一个表中选择一组记录,其中的字段是另一个表中记录的子集。 Specifically... 特别...

​The team_competition table stores the competitions a team is registered for: team_competition表存储团队注册的竞赛:

Team Competition 团体比赛

630 AGLOA2017ELCE 630 AGLOA2017ELCE

630 AGLOA2017ELEQ 630 AGLOA2017ELEQ

630 AGLOA2017ELPREZ 630 AGLOA2017ELPREZ

630 AGLOA2017ELPROP​ 630 AGLOA2017ELPROP

... ...

The player_competition table stores the competitions a player is registered for: player_competition表存储玩家注册的比赛:

Player Competition 球员比赛

186 AGLOA2017SREQ 186 AGLOA2017SREQ

186 AGLOA2017SRLING 186 AGLOA2017SRLING

186 AGLOA2017SROS 186 AGLOA2017SROS

186 AGLOA2017SRPREZ 186 AGLOA2017SRPREZ

186 AGLOA2017SRPROP 186 AGLOA2017SRPROP

186 AGLOA2017SRWFF 186 AGLOA2017SRWFF

191 AGLOA2017SREQ 191 AGLOA2017SREQ

191 AGLOA2017SRLING 191 AGLOA2017SRLING

191 AGLOA2017SROS 191 AGLOA2017SROS

191 AGLOA2017SRPREZ 191 AGLOA2017SRPREZ

191 AGLOA2017SRPROP 191 AGLOA2017SRPROP

191 AGLOA2017SRWFF 191 AGLOA2017SRWFF

... ...

To assign players to teams, I need to select players who are registered for the same competitions as the team in question. 为了将球员分配到球队,我需要选择与相关球队注册相同比赛的球员。 For team 630 in this example, I need to select players who are registered for at least those four competitions: AGLOA2017ELCE, AGLOA2017ELEQ, AGLOA2017ELPREZ, and AGLOA2017ELPROP​. 对于该示例中的团队630,我需要选择至少注册那四个比赛的球员:AGLOA2017ELCE,AGLOA2017ELEQ,AGLOA2017ELPREZ和AGLOA2017ELPROP。

Mathematically, I think this is equivalent to selecting players' competitions that are a superset (contain) of the team's competitions. 在数学上,我认为这相当于选择球员比赛的超级(包含)的球员比赛。 I'm currently using a moderately complicated routine to loop through players and competitions and compare each one to the team's competitions. 我目前正在使用一个中等复杂的例程来循环玩家和比赛,并将每个比较与球队的比赛进行比较。 It works, but seems grossly inefficient. 它有效,但似乎非常低效。

I'm studying how to use MySQL to select 'subsets,' but I've hit a mental block. 我正在研究如何使用MySQL来选择“子集”,但我遇到了一个心理障碍。 When that happens, I can usually make some progress by writing down my question. 当发生这种情况时,我通常可以通过写下我的问题来取得一些进展。 Just typing this has given me some ideas, but now that it's written... 只是键入这个给了我一些想法,但现在已经写了......

... thanks in advance for sharing any solutions you may have. ...提前感谢您分享您可能拥有的任何解决方案。

I'm not sure this is a MySQL best practice but you can achieve this query by comparing counts of competitions for a team to counts of teams joined with players. 我不确定这是一个MySQL最佳实践,但你可以通过比较一个团队的竞争计数和加入玩家的团队数量来实现这个查询。

Select Team, Player
From (
    Select Team, Player, count(0) matching 
    From team_competition t
        Inner join player_competition p on t.Competition = p.Competition
    Group by Team, Player
) tp 
Inner join (
    Select Team, count(0) competitions
    From team_competitions
    Group by Team
) t on tp.Team = t.Team
Where tp.matching = t.competitions

(Wrote this on a phone, forgive formatting) (在手机上写这个,原谅格式化)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM