0

I am trying to add the value of a column into a SQL CONCAT query to be able to update another column, let me demonstrate.

enter image description here

I need the value of the column "winner" and add it to my query, since what I want to accomplish is for the query to say SELECT team2_name FROM x WHERE matchid = 1.

This is what I've tried so far.

UPDATE tournament_matches 
SET match_id = 5 
WHERE team_name IN (
    SELECT CONCAT(@winner, '_name') 
    FROM get5_stats_matches 
    WHERE match_id = 1
);

But it doesn't affect any row since CONCAT(@winner, '_name') doesn't display team2_name which I need it to do.

Where do I go wrong?

7
  • Why do you have @ before winner? That's using a user-defined variable, not the table column. Commented Mar 5, 2021 at 15:45
  • Change @winner to winner. Commented Mar 5, 2021 at 15:46
  • If I just type winner + '_name' it will update 2 rows instead of just one. Commented Mar 5, 2021 at 15:51
  • Please post sample data from both tables and the desired result. Commented Mar 5, 2021 at 15:52
  • It should be CONCAT(winner, '_name'), not winner + '_name' Commented Mar 5, 2021 at 15:53

1 Answer 1

1

I think this is what you want:

UPDATE tournament_matches
SET match_id = 5
WHERE team_name IN (
    SELECT IF(winner = 'team1', team1_name, team2_name)
    FROM get5_stats_matches
    WHERE match_id = 1);

You can't use an expression to specify a column name directly.

Demos:

Winner = Team1

Winner = Team2

Sign up to request clarification or add additional context in comments.

11 Comments

This seems to be working quiet good, however. It updates 2 rows when the SELECT IF statement goes to false, why is that?
There must be two rows with team_name = 'Svertila'. Which one should it update if it doesn't update both?
It's really just one row with team_name = 'Svartlila', I tried the SELECT IF(winner = 'team1', team1_name, team2_name) and it worked fine and gave back the correct results, when I add it to the whole query, it only works if team1 is the winner, if we change the column value so that team2 is the winner, 2 rows updates.
What is the team name of the 2nd row that's being updated?
I asked you to provide sample data, I'll ask again.
|

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.