How to use a variable as a column name for a SELECT statement

Solution 1:

To use variables to represent columns (or tables), you need to use PREPARE/EXECUTE. Try something like this:

    SET @colindex1=(CONCAT(i, @indent);    
    SET @stmt = 'SELECT @colindex1 FROM tableindex WHERE main=19';
    PREPARE thestmt FROM @stmt;
    EXECUTE thestmt;