"mysql - concat - is there any way to concat a string and use it as a variable?" Code Answer

3

if you have variable column name, you will need to use dynamic sql:

set @strokes_hole_10 = 6;
set @x = 10;
set @strokes = concat('@strokes_hole_',@x); -- add @ to variable string

-- generate the query string
set @query_str = concat('select ', @strokes);

-- prepare statement using the query string
prepare stmt from @query_str;

-- executes the prepared statement
execute stmt;

-- clean up after execution
deallocate prepare stmt;

result

| @strokes_hole_10 |
| ---------------- |
| 6                |

view on db fiddle

By Aditya Arora on June 28 2022

Answers related to “mysql - concat - is there any way to concat a string and use it as a variable?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.