[英]How to make SQL Query with Two Different Table
I cannot fine same question in here, so I write it.我不能在这里解决同样的问题,所以我写了它。
I need one SQL query for below details;我需要一个 SQL 查询以获取以下详细信息; I have two tables and 2st table has team_id and 2nd table have home_team_id and away_team_id and home and away scores.我有两张桌子,第二张桌子有 team_id,第二张桌子有 home_team_id 和 away_team_id 以及主客场得分。
I can make home and away matches separately as below queries already:我已经可以按照以下查询分别进行主场和客场比赛:
Home Matches Query:主场比赛查询:
SELECT Distinct m.week,m.match_date, h.team_name as Home, a.team_name as AWAY,m.home_score,m.away_score,m.league_id
FROM matches m,teams h,teams a, leagues l
WHERE h.team_id=m.home_team_id and a.team_id=m.away_team_id
and h.team_name ='Real Madrid' and m.league_id = 1;
Away Matches Query:客场匹配查询:
SELECT Distinct m.week,m.match_date, h.team_name as Home, a.team_name as AWAY,m.home_score,m.away_score,m.league_id
FROM matches m,teams h,teams a, leagues l
WHERE h.team_id=m.home_team_id and a.team_id=m.away_team_id
and a.team_name ='Real Madrid' and m.league_id = 1;
I want to see one team home and away results in same query.我想看到一支球队的主场和客场结果在同一个查询中。
My Tables are: (matches and teams)我的桌子是:(比赛和球队)
matches table columns are;匹配表列是; match_id match_date, home_team_id, away_team_id, home_score, away_score match_id match_date, home_team_id, away_team_id, home_score, away_score
teams table columns are;团队表列是; team_id, team_name team_id,team_name
I want to see below result:我想看看下面的结果:
One team's Home and Away score in one table as一支球队的主客场得分在一张表中为
Example: Team is Real Madrid示例:球队是皇家马德里
Results:结果:
Home Team主队 | Away Team客队 | Home Score主场比分 | Away Score客场得分 |
---|---|---|---|
Real Madrid皇家马德里 | Barcelona巴塞罗那 | 1 1 | 0 0 |
Real Madrid皇家马德里 | Cadiz加的斯 | 1 1 | 1 1 |
Real Madrid皇家马德里 | A. Madrid A. 马德里 | 2 2 | 2 2 |
Villareal比利亚雷亚尔 | Real Madrid皇家马德里 | 2 2 | 0 0 |
Valencia瓦伦西亚 | Real Madrid皇家马德里 | 1 1 | 3 3 |
You have to join two tables with teams table and matches table.您必须将两个表与团队表和匹配表连接起来。 Use OR paramater instead of AND.使用 OR 参数而不是 AND。
Use the same query, but test the team name against either h.team_name
or a.team_name
.使用相同的查询,但根据h.team_name
或a.team_name
测试团队名称。
SELECT Distinct m.week,m.match_date, h.team_name as Home, a.team_name as AWAY,m.home_score,m.away_score,m.league_id
FROM matches m
JOIN teams h ON h.team_id=m.home_team_id
JOIN teams a ON a.team_id=m.away_team_id
WHERE 'Real Madrid' IN (h.team_name, a.team_name) and m.league_id = 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.