0

TL;DR

I need to select two rows and 2 columns from each row from Table Matches_teams and update one row and 4 columns in Table Matches.

My issue:

Consider this example: a football fantasy-like database. There are football teams, matches which must always include two different teams and each team will have odds

I have the following tables:

Table Matches:

Matches
-------- 
ID | date | team_winner_id | score

Then I created joint table Matches_teams. The following, for each match there it must always have 2 different teams with their respective odds

Matches_teams
--------------
ID | team_id | match_id | odds 

Example records for the Matches_teams table:

 ID | team_id | match_id | odds
-------------------------------
 1  |    5    |    22     | 0.8
 2  |    6    |    22     | 2.2
 3  |   12    |    56     |  1
 4  |    3    |    56     | 2.1

Now I realised that the joint table Matches_teams is not really needed. If a match can only and will always have two teams, I can add a column for each team and its odds in Matches table like this

Matches
-------- 
ID | team_a_id | team_a_odds | team_b_id | team_b_odds | date | team_winner_id | score

Using the above Matches_teams data as an example, I want my restructured Matches data to look like this:

Matches
ID | team_a_id | team_a_odds | team_b_id | team_b_odds | date | team_winner_id | score
---------------------------------------------------------------------------------------
22 |      5    |      0.8    |     6     |     2.2     |  ..  | ....
56 |     12    |       1     |     3     |     2.1     |  ..  | ....

Any MySQL operation which can do this? An operation which by a given match id, it will update Matches's existing records based on the data selected from Matches_teams and on the condition that Matches.id equals Matches_teams.match_id

Matthew Barbara
  • 3,792
  • 2
  • 21
  • 32
  • P.S. The answer found in 'marked as duplicated' does not solve my problem. I have two rows and 2 columns each row from Table_A and update one row and 4 columns in Table_B. – Matthew Barbara Nov 13 '18 at 22:48
  • Assuming Table_A is teams you join twice, `JOIN team AS teama .... JOIN team AS teamb`. Btw jumping between Table_A/B notation and the very real structure you've provided is hard to correlate. Please stick to a single notation. – danblack Nov 13 '18 at 23:11
  • Thanks for your reply @danblack. TL;DR version updated. The problem with multiple join is that on my second join, matches_team_2 I am getting the same data as from the first join. with `JOIN matches_team as mt1 ... JOIN mathces_team as mt2`, `m1.team_id` is the same as `m2.team_id` which is not what I need. If i can get the second team's ID I would be able to solve my issue – Matthew Barbara Nov 13 '18 at 23:13
  • Show the query, you may need a `matches_team_1.team_id < matches_team_2.team_id` criteria. – danblack Nov 13 '18 at 23:17
  • `matches_team_1.team_id < matches_team_2.team_id` Did the trick! what is happening exactly there? I'd be more than happy to accept your answer as the official answer to my question. Thanks @danblack – Matthew Barbara Nov 13 '18 at 23:30
  • This is my working query: `update matches JOIN matches_teams as mt1 ON matches.id = mt1.match_id JOIN matches_teams as mt2 ON matches.id = mt2.match_id SET matches.team_a = mt1.team_id, matches.team_a_odds = mt1.odds, matches.team_b = mt2.team_id, matches.team_b_odds = mt2.odds where matches.id != 0 and mt1.team_id < mt2.team_id` using where matches.id != 0 to updating all records – Matthew Barbara Nov 13 '18 at 23:36
  • I can't do an answer, already marked as duplicate. The `mt1.team_id < mt2.team_id` places enough constraints (because your data only ever has two rows for this table where the team_id isn't the same) to reduce the `JOIN`s down to having a single row answer for each matches.id. – danblack Nov 14 '18 at 00:42

0 Answers0