[英]SQL complicated join query
I have two tables: match(id.match, date, home, away)
and team(id. team, name)
. 我有两个表: match(id.match, date, home, away)
和team(id. team, name)
match(id.match, date, home, away)
team(id. team, name)
。 In match
home
and away
are foreign keys to the team id
. 在match
home
和away
均设有team id
外键。 In my query, I want an output record with date
of match,
and name
of these two teams. 在我的查询中,我需要具有match,
date
和这两支球队name
的输出记录。
I have tried: 我努力了:
SELECT m.date, m.home, m.away, t.name
FROM `match` m
JOIN team t ON m.home = t.id_team
ORDER BY m.date
But this outputs two records instead of one. 但这输出两个记录而不是一个。 Is it possible to do what I want with sql or I should just change the table's design? 是否可以用sql做我想做的事情,或者只更改表的设计?
I suspect you have a many to one relationship between your tables. 我怀疑您的表之间存在多对一的关系。
This might work depending on the table values: 这可能取决于表值:
SELECT DISTINCT m.date, m.home, m.away, t.name
FROM `match` m JOIN
team t ON m.home = t.id_team
ORDER BY m.date
Ok, i did 'deeper' research and find out the answer. 好吧,我做了“更深入的研究”并找出了答案。 Earlier i just asked wrong question. 之前我只是问错了问题。 Thanks everyone for contribution! 谢谢大家的贡献!
Here is the solution: 解决方法如下:
SELECT m.date, m.home, m.away, t.name AS homename, te.name AS awayname
FROM `match` m
JOIN team t ON m.home = t.id_team
JOIN team te
ON m.away = te.id_team ORDER BY m.date
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.