在一行中显示Oracle中多个表的数据

时间:2017-06-09 14:14:47

标签: sql oracle

我有3张表如下:

table_teams id_team ,姓名,城市)

table_matches id_match ,date_of_match,city)

table_game id ,id_team(FK),id_match(FK),目标) - 这一个是前两个之间的联结表

同一场比赛被引入table_game作为两个单独的记录,每个记录包含一个目标数量,但具有相同的id_match,

INSERT INTO table_game(id_team, id_match, goals) VALUES('GER', 1234, 7);
INSERT INTO table_game(id_team, id_match, goals) VALUES('BRA', 1234, 1);

并作为table_matches中的单个记录,

INSERT INTO table_matches(id_match, date_of_match, city)
VALUES (1234, '8-JUL-2014', 'Belo Horizonte');

现在,我想创建一个视图,从这些表中选择数据并按如下方式显示:

  

日期| Team1 |目标1 |目标2 | Team2 |城市

我到目前为止所做的最好的事情是在表格的不同行显示每条记录。

SELECT tm.date_of_match, tt.name, tg.goals, tm.city
FROM table_matches tm, table_teams tt, table_game tg
WHERE tm.id_match = tg.id_match AND tt.id_team = tg.id_team;

但我不知道如何显示如上所述的数据。我所有的尝试都造成了很大的混乱。

会感激一些帮助。谢谢。

3 个答案:

答案 0 :(得分:0)

您可以使用ROW_NUMBER分配值然后加入它,如下所示:

;with cte (team,match,goals,row) as 
(
select
    id_team,
    id_match,
    goals,
    ROW_NUMBER () over (partition by match_id order by team_id)
from table_game
)

select 
t1.date_of_match,c.team, c.goals, c1.goals, c1.team, t1.city
from cte c
left join cte c1 on c.match = c1.match and c1.row = 2
inner join table_matches t1 on c.match = t1.id_match
where c.row = 1

答案 1 :(得分:0)

使用JOIN语法

-- Test data
WITH table_game(ID, id_team, id_match, goals) AS (
         SELECT 10, 'USA',  100, 5 FROM dual UNION ALL -- error (3 records for same match)
         SELECT 11, 'RUS',  100, 4 FROM dual UNION ALL -- error (3 records for same match)
         SELECT 12, 'BRA',  100, 3 FROM dual UNION ALL -- error (3 records for same match)
         SELECT 20, 'ARG', 1234, 7 FROM dual UNION ALL
         SELECT 21, 'BRA', 1234, 1 FROM dual UNION ALL
         SELECT 30, 'ENG', 4321, 6 FROM dual UNION ALL
         SELECT 31, 'FRA', 4321, 2 FROM dual
         )
,table_matches(id_match, date_of_match, city) AS (
         SELECT 1234, DATE '2014-07-08', 'Belo Horizonte' FROM dual UNION ALL
         SELECT 4321, DATE '2014-08-09', 'Sao Paulo'      FROM dual UNION ALL
         SELECT  100, DATE '2014-09-10', 'Brasilia'       FROM dual)
,table_teams(id_team, NAME, city) AS (
         SELECT 'ARG', 'Argentina', 'Buenos Aires' FROM dual UNION ALL
         SELECT 'BRA', 'Brazil', 'Sao Paulo'       FROM dual UNION ALL
         SELECT 'ENG', 'England', 'London'         FROM dual UNION ALL
         SELECT 'FRA', 'France', 'Paris'           FROM dual UNION ALL
         SELECT 'RUS', 'Russia', 'Moscow'          FROM dual UNION ALL
         SELECT 'USA', 'United States', 'New York' FROM dual)
-- End Test data
,match_teams AS (
  SELECT id_match, MAX(id_team) team1, MIN(id_team) team2
  FROM   table_game
  GROUP  BY  id_match
  HAVING COUNT(*) = 2 -- any valid match should have only 2 teams, otherwise ignored 
)
SELECT M.date_of_match, t1.NAME team1, g1.goals goals1, t2.NAME team2, g2.goals goals2, M.city city_match 
FROM table_matches M
  JOIN match_teams mt ON mt.id_match = M.id_match
  JOIN table_game  g1 ON g1.id_match = M.id_match AND g1.id_team = mt.team1
  JOIN table_game  g2 ON g2.id_match = M.id_match AND g2.id_team = mt.team2
  JOIN table_teams  t1 ON t1.id_team = mt.team1
  JOIN table_teams  t2 ON t2.id_team = mt.team2
/

加入WHERE子句

-- Test data
WITH table_game(ID, id_team, id_match, goals) AS (
         SELECT 10, 'USA',  100, 5 FROM dual UNION ALL -- error (3 records for same match)
         SELECT 11, 'RUS',  100, 4 FROM dual UNION ALL -- error (3 records for same match)
         SELECT 12, 'BRA',  100, 3 FROM dual UNION ALL -- error (3 records for same match)
         SELECT 20, 'ARG', 1234, 7 FROM dual UNION ALL
         SELECT 21, 'BRA', 1234, 1 FROM dual UNION ALL
         SELECT 30, 'ENG', 4321, 6 FROM dual UNION ALL
         SELECT 31, 'FRA', 4321, 2 FROM dual
         )
,table_matches(id_match, date_of_match, city) AS (
         SELECT 1234, DATE '2014-07-08', 'Belo Horizonte' FROM dual UNION ALL
         SELECT 4321, DATE '2014-08-09', 'Sao Paulo'      FROM dual UNION ALL
         SELECT  100, DATE '2014-09-10', 'Brasilia'       FROM dual)
,table_teams(id_team, NAME, city) AS (
         SELECT 'ARG', 'Argentina', 'Buenos Aires' FROM dual UNION ALL
         SELECT 'BRA', 'Brazil', 'Sao Paulo'       FROM dual UNION ALL
         SELECT 'ENG', 'England', 'London'         FROM dual UNION ALL
         SELECT 'FRA', 'France', 'Paris'           FROM dual UNION ALL
         SELECT 'RUS', 'Russia', 'Moscow'          FROM dual UNION ALL
         SELECT 'USA', 'United States', 'New York' FROM dual)
-- End Test data
,match_teams AS (
  SELECT id_match, MAX(id_team) team1, MIN(id_team) team2
  FROM   table_game
  GROUP  BY  id_match
  HAVING COUNT(*) = 2 -- any valid match should have only 2 teams, otherwise ignored 
)
SELECT M.date_of_match, t1.NAME team1, g1.goals goals1, t2.NAME team2, g2.goals goals2, M.city city_match 
FROM   table_matches M
      ,match_teams mt 
      ,table_game  g1
      ,table_game  g2
      ,table_teams  t1
      ,table_teams  t2
WHERE  mt.id_match = M.id_match
 AND   g1.id_match = M.id_match 
 AND   g1.id_team = mt.team1
 AND   g2.id_match = M.id_match 
 AND   g2.id_team = mt.team2
 AND   t1.id_team = mt.team1
 AND   t2.id_team = mt.team2
/

答案 2 :(得分:0)

<div class="about-container">
  <div class="about-text">
    <h3 id="title">Title</h3>
    <p> Lorem ipsum dolor sit amet</p>
  </div>
  <div class="pic"></div>
</div>
<div class="section-container">
  <h3>
    Section Title
  </h3>
  Some text for the next section
</div>

试验:

select date_of_match, g1.id_team team1, g1.goals goals1, 
       g2.goals goals2, g2.id_team team2, m.city
  from matches m  
  join games g1 on g1.id_match = m.id_match
  join games g2 on g1.id_match = m.id_match and g2.id > g1.id

结果:

with games(id, id_team, id_match, goals) as (
         select 1, 'GER', 1234, 7 from dual union all
         select 2, 'BRA', 1234, 1 from dual),
     matches(id_match, date_of_match, city) as (
         select 1234, date '2014-07-08', 'Belo Horizonte' from dual)
select date_of_match, g1.id_team team1, g1.goals goals1, 
       g2.goals goals2, g2.id_team team2, m.city
  from matches m  
  join games g1 on g1.id_match = m.id_match
  join games g2 on g1.id_match = m.id_match and g2.id > g1.id