简体   繁体   English

如何使用两个不同的表进行 SQL 查询

[英]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_namea.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.

相关问题 如何查询一个表的同一列中的两个不同对象,这些对象与另一个表中的不同列相关? - How can i make a query of two different objects in the same column of one table, relationed to different columns in another table? 如何在sql查询中匹配两个不同的表? - How to match two different tables in sql query? 如何在SQL中合并两个不同的选择查询 - How to merge two different select query in sql 当单个表的两个不同属性的值相同时,如何进行mysql查询? - How to make a mysql query when a single table have the same value for two different attributes? 如何在SQL中合并具有不同列的两个表 - how to merge two table with different columns in sql 如何对不同的表执行多个SQL查询? - How to execute multiple SQL query to different table? 如何使来自同一表的两个$ db-> query具有按列彼此独立地位于一个php页面中的两个不同ORDER? - How to make two $db->query from the same table with two different ORDER by column that each other stand independenty in one php page? sql查询以获取两个不同表的两行的乘法,并将答案插入两个表之一 - sql query to get multiplication of two rows of two different tables and insert the answer to one of the two table 慢速SQL查询:在两个不同的联接中使用同一张表会使查询变慢10倍! - Slow SQL query: using the same table in two different joins causes query to become 10x slower! 如何在单个查询中将两个不同的数组值插入表中 - How to insert two different array values into a table in a single query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM