A
A
Arthaus252020-12-25 20:45:04
SQL
Arthaus25, 2020-12-25 20:45:04

Sampling from two tables taking into account other communications?

There is such a database
5fe621ac88cbe659610637.png

How to write a query to get the following columns for output:
Grands_Prix.Round
Grands_Prix.Title
Grands_Prix.Country
Grands_Prix_Racers.Position
Racers.Name
Teams.Title

If I need the output to be exclusively for one team? It's either Teams.Title, or Teams.id (or Racers_Teams_Seasons.Teams_Id?)

This query gives a very strange result: it displays each race of any racer, while giving different positions for the same race, but it correctly writes the same one for everyone the same command, which is not true.

SELECT Grands_Prix.Round,
    Grands_Prix.Title,
    Grands_Prix.Country,
    Grands_Prix_Racers.Position,
    Racers.Name,
    Teams.Title,
    Teams.Id
FROM Grands_Prix, Grands_Prix_Racers, Racers, Teams
WHERE Teams.id = 6


This is not visible on the screen of the response to the request, but note that the lines in the response are 693,120.

There are two riders in the team, 38 races, which means that logically, there should be 76 results.

5fe624adc02d5099549645.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arthaus25, 2020-12-25
@Arthaus25

In the end, I created a request like this and it works. I apologize for the stupid comments, I wrote for myself.

SELECT
  -- Берём Раунд (какое Гран-При по счёту), Автодром, Страну и Где автодром находится, Гонщика, \
  -- его Финальную позицию и Команду
  E.Round, E.Circuit, E.Country, E.Location, F.Name, F.Position, F.Title 
FROM 
  Grands_Prix as E -- ЭТО ТАБЛИЦА Е!
-- Джойним (справа, по id Гран-При) таблицу Гран-при и F
RIGHT OUTER JOIN 
  (
  SELECT
    -- Берём id гонки (Гран-При), позицию пилота на финише, его имя и название команды
    C.Grands_Prix_Id, C.Position, D.Name, D.Title 
  -- Джойним (справа, по id пилотов) таблицу связи Гран-При--пилотов \
  -- с таблицей А (см. то, что в RIGHT OUTER JOIN)
  FROM
    Grands_Prix_Racers as C -- ЭТО ТАБЛИЦА С!
  RIGHT OUTER JOIN
    (
      SELECT 
        -- Берём id пилота, его имя и название команды
        A.Id, A.Name, B.Title 
      FROM 
        (
        SELECT	
          -- Берём id пилота, его имя, id команды, сезон 
          Racers.Id, Racers.Name, Racers_Teams_Seasons.Teams_Id, Racers_Teams_Seasons.Seasons_Year 
        -- Джойним (справа, по id команд) таблицы гонщиков и таблицу связей гонщиков-коман-сезонов 
        FROM 
          Racers
        RIGHT OUTER JOIN 
          Racers_Teams_Seasons
        ON 
          Racers.Id = Racers_Teams_Seasons.Racer_Id
        WHERE
          -- по условию, сезон — 2020, Команда по id — 3 (Racing Point)
          (Seasons_Year = 2020) AND (Teams_Id = 3) 
        ) as A -- В СКОБКАХ — ЭТО ТАБЛИЦА А!

      LEFT OUTER JOIN
        Teams as B -- ЭТО ТАБЛИЦА В!
      ON
        A.Teams_Id = B.Id 
    ) as D -- ЭТО ТАБЛИЦА D!
  ON
    C.Racers_Id = D.Id
  WHERE
    -- Берём Гран-При с id выше 21 \
    -- В 2019 году было 21 Гран-При, следовательно, \
    -- так берём все ГП 2020 года 
    -- [Я знаю, это косяк архитектуры. \
    -- На будущее — перестать запрещать изменения в любых таблицах!])
    C.Grands_Prix_Id > 21  
                 
  ) as F -- ЭТО ТАБЛИЦА F!
ON
  E.Id = F.Grands_Prix_Id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question