简体   繁体   English

MySQL连接带有条件的表

[英]MySQL Join tables with a condition

My SQL isn't up to this, but I'm pretty sure it is easy for an SQL guru - I'd rather figure out the right query rather than putting the logic into a PHP script... 我的SQL不是这个,但我很确定SQL大师很容易 - 我宁愿找出正确的查询,而不是把逻辑放到PHP脚本中......

So: I have 2 tables, one contains info about a club, a specific match and how many teams the club has entered for that match. 所以:我有两张桌子,一张包含一个俱乐​​部的信息,一个特定的比赛以及俱乐部为这场比赛输入的球队数量。 The second table splits the entries into "divisions" 第二个表将条目拆分为“分区”

ie

mysql> describe Entries;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| ClubId   | int(11) | NO   | PRI | NULL    |       |
| MatchId  | int(11) | NO   | PRI | NULL    |       |
| NumTeams | int(11) | NO   |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
mysql> describe Divisions;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| MatchId  | int(11) | NO   | MUL | NULL    |       |
| ClubId   | int(11) | NO   | MUL | NULL    |       |
| Team     | int(11) | NO   |     | NULL    |       |
| Division | int(11) | NO   |     | NULL    |       |
+----------+---------+------+-----+---------+-------+

Sample data: 样本数据:

Entries:
1, 1, 1 (Club1, Match1, 1 team)
1, 3, 2 (Club1, Match3, 2 teams)

Divisions:
1, 1, 1, 1 (Club1, Match1, Team1 is in division 1)
1, 1, 2, 2 (Club1, Match1, Team2 is in division 2)
1, 2, 1, 1 (Club1, Match2, Team1 is in division 1)
1, 2, 2, 2 (Club1, Match2, Team2 is in division 2)
1, 3, 1, 1 (Club1, Match3, Team1 is in division 1)
1, 3, 2, 2 (Club1, Match3, Team2 is in division 2)

Required result: 要求的结果:

1, 1, 1 (Club 1, Match1, Team1)
1, 3, 1 (Club1, Match3, Team1)
1, 3, 2 (Club1, Match3, Team2)

- so not all the rows in Divisions are in the result.
  e.g, club1, match1, team2 isn't here because there
  is only one entry in match 1.

Now Entries.NumTeams can go up and down as a club enters or removes teams. 现在,当俱乐部进入或移除球队时,Entries.NumTeams可以上下起伏。 When a new team goes in, it gets put in division 1 (just so it is always in a division). 当一个新团队进入时,它会被放入第1部分(就这样它总是在一个部门中)。 When teams are removed, their entry in the Divisions table IS NOT removed (divisions are set manually, so if someone has decided Club X's 2nd team belongs in division 2, you want to keep that information even if Club X have not yet entered a 2nd team). 当团队被移除时,他们在分区表中的输入不会被删除(分区是手动设置的,所以如果有人决定俱乐部X的第二队属于分区2,即使俱乐部X还没有进入第二队,你也希望保留这些信息球队)。

So, to print all teams in a division I need something like: 所以,要打印一个部门的所有团队,我需要这样的东西:

SELECT * FROM Divisions WHERE Divisions.Team <= Entries.NumTeams
    (for the Entries row with the matching match and club)

I tried this: 我试过这个:

SELECT Divisions.* from Entries LEFT JOIN Divisions ON 
    Divisions.MatchId = Entries.MatchId AND Divisions.ClubId = Entries.ClubId 
    WHERE Divisions.Team <= Entries.NumTeams

But I ended up with duplicate rows. 但我最终得到了重复的行。 I could probably get rid of them by using SELECT DISTINCT, but that makes me think my query is broken to start off with. 我可以通过使用SELECT DISTINCT来摆脱它们,但这让我觉得我的查询已经被打破了。 I also swapped which table was left and right, but still got duplicates. 我还交换了哪个表左右,但仍然有重复。

Will try the provided answers tomorrow. 将在明天尝试提供的答案。 Thanks! 谢谢!

使用DISTINCT将删除重复行,您的查询似乎没问题。

use distinct in mysql 在mysql中使用distinct

SELECT distinct Divisions.* from Entries LEFT JOIN Divisions ON 
    Divisions.MatchId = Entries.MatchId AND Divisions.ClubId = Entries.ClubId 
    WHERE Divisions.Team <= Entries.NumTeams

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

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