Mysql多对多查询
我的 mysql 数据库中有多对多表设置。球队可以参加多场比赛,每场比赛有 2 支球队。它们之间有一个名为 team_games 的表。显示创建表信息如下。
我已经搞乱这个查询有一段时间了。此时我不关心它是否需要子查询、连接或联合。我已经尝试了所有这些,但没有一个是令人满意的,我想我错过了一些东西。我一直遇到的问题是从每场比赛中找到两个球队 ID,然后使用 tid 来获取球队信息。
我想做的是,如果给定一个游戏ID(gid),我可以查询找到:
的所有信息
home_team_name,home_team_id,away_team_name,away_team_id,team_league(客场和主场将是同一个联赛),来自游戏表表创建表
teams CREATE TABLE `teams` (
`tid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(60) NOT NULL,
`league` varchar(2) NOT NULL,
`active` tinyint(1) NOT NULL,
PRIMARY KEY (`tid`)
)
> <代码> 创建表teams_games
(
`tid` int(10) unsigned NOT NULL,
`gid` int(10) unsigned NOT NULL,
`homeoraway` tinyint(1) NOT NULL,
PRIMARY KEY (`tid`,`gid`),
KEY `gid` (`gid`),
CONSTRAINT `teams_games_ibfk_1` FOREIGN KEY (`tid`) REFERENCES `teams` (`tid`),
CONSTRAINT `teams_games_ibfk_2` FOREIGN KEY (`gid`) REFERENCES `games` (`gid`)
)
> <代码> 创建表游戏
(
`gid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`location` varchar(60) NOT NULL,
`time` datetime NOT NULL,
`description` varchar(400) NOT NULL,
`error` smallint(2) NOT NULL,
`home_score` smallint(2) DEFAULT NULL,
`away_score` smallint(2) DEFAULT NULL,
PRIMARY KEY (`gid`)
)
I have a many to many table setup in my mysql database. Teams can be in many games and each game has 2 teams. There is a table in between them called teams_games. SHOW CREATE TABLE information follows.
I have been messing with this query for a while. At this point I don't care if it requires sub-queries, joins, or unions. I have tried all of them but nothing has been satisfactory and I think i'm missing something. The disconnect I keep having is finding the two team ids from each game and then using the tid to grab the team information.
What I would like to do is if given a game id (gid) I can query to find:
home_team_name, home_team_id, away_team_name, away_team_id, team_league(away and home will be the same league), all the information from games table
Table Create Table
teams CREATE TABLE `teams` (
`tid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(60) NOT NULL,
`league` varchar(2) NOT NULL,
`active` tinyint(1) NOT NULL,
PRIMARY KEY (`tid`)
)
CREATE TABLE teams_games
(
`tid` int(10) unsigned NOT NULL,
`gid` int(10) unsigned NOT NULL,
`homeoraway` tinyint(1) NOT NULL,
PRIMARY KEY (`tid`,`gid`),
KEY `gid` (`gid`),
CONSTRAINT `teams_games_ibfk_1` FOREIGN KEY (`tid`) REFERENCES `teams` (`tid`),
CONSTRAINT `teams_games_ibfk_2` FOREIGN KEY (`gid`) REFERENCES `games` (`gid`)
)
CREATE TABLE games
(
`gid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`location` varchar(60) NOT NULL,
`time` datetime NOT NULL,
`description` varchar(400) NOT NULL,
`error` smallint(2) NOT NULL,
`home_score` smallint(2) DEFAULT NULL,
`away_score` smallint(2) DEFAULT NULL,
PRIMARY KEY (`gid`)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不直接删除
teams_games
表并更改games
:然后您可以编写一个简单的联接,例如:
Why not just drop the
teams_games
table and altergames
:Then you can write a simple join like:
我假设 homeoraway = 1 表示主场,homeoraway = 0 表示客场。
这是通过将主队的比赛加入到 team_games 中,然后加入到球队中来获取球队信息,然后为客队做同样的事情来实现的。
I assumed homeoraway = 1 for home and homeoraway = 0 for away.
This works by joining games to team_games for the home team, then to teams for the team info, then doing the same thing for the away team.
我假设 1 = 在家,2 = 离开。你可以适当改变。
I assumed that 1 = home, 2 = away. You can change appropriately.