3

I am trying to update a table based on the values in the table already.

Player Fixture

PlayerFId
FixtureFId
Availability
SelectedPosition

I want to update all rows where the player id is the same and take the values from a previous fixture id.

Example data

PlayerFId Fixture FId Availability SelectedPosition
    1           1          N              2
    1           2          U              0
    2           1          A              3
    2           2          U              0

I want to update all the rows for FixtureFId 2 with the relevant PlayerFId data for FixtureId 1.

End Result

PlayerFId Fixture FId Availability SelectedPosition
        1           1          N              2
        1           2          N              2
        2           1          A              3
        2           2          N              3    

I have tried using this SQL but it doesn't map the PlayerFId values correctly

UPDATE player_fixture 
SET 
    Availability = (SELECT Availability FROM player_fixture WHERE FixtureFId = 1), 
    SelectedPosition = (SELECT SelectedPosition FROM player_fixture WHERE FixtureFId = 1) 
WHERE FixtureFId = 2
3
  • Where is this part reflected in he query: "where the player id is the same"? Commented Sep 23, 2018 at 16:36
  • That's where I'm struggling, I'm guessing it needs to be a self join? Commented Sep 23, 2018 at 16:37
  • You can reference the outer table in the inner query, just use an alias. Commented Sep 23, 2018 at 16:38

2 Answers 2

6

SQLite does not allow joins in updates. But it does allow you to update multiple columns with a single subquery:

UPDATE player_fixture
       SET (availability, selectedposition) =
            (SELECT pf.availability, pf.selectedposition
             FROM player_fixture pf
             WHERE pf.playerfid = player_fixture.playerfid AND
                   pf.fixturefid = 1
            )
       WHERE fixturefid = 2;
Sign up to request clarification or add additional context in comments.

1 Comment

@Carl . . . I'm surprised you accepted and then unaccepted this answer.
1

You need to include the player's ID in the WHERE clause of the subqueries so that the result correlates.

UPDATE player_fixture
       SET availability = (SELECT pf.availability
                                  FROM player_fixture pf
                                  WHERE pf.playerfid = player_fixture.playerfid
                                        AND pf.fixturefid = 1), 
           selectedposition = (SELECT pf.selectedposition
                                      FROM player_fixture pf
                                      WHERE pf.playerfid = player_fixture.playerfid
                                            AND pf.fixturefid = 1)
       WHERE fixturefid = 2;

db<>fiddle

10 Comments

This sets both columns to null.
@Cael Try setting the alias on the outer table reference not on the inner ones.
It works if adapted to your sample data: dbfiddle.uk/… Maybe there is no fixture with a one less ID in your real data? See my edit on how you can try to cover this.
I tried setting the FixtureFId to a specific value on the inner query e.g. 1. Why does it have to be minus 1?
Problem is the fixture id's could be completely random and some previous fixtures with higher value FixtureFId.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.