If the string is in your external application (such as PHP), of course, just construct the MySQL statement.
Not if the string is inside a MySQL table. MySQL does not have eval() or such functions. The following is not possible:
Suppose you have a table queries in which field columnname refers to one of the column names in table mytable. There may be additional columns in the query that allow you to select the desired column names.
INSERT INTO queries (columname) VALUES ("name")
SELECT (select columnname from queries) from mytable
SELECT columnname from queries into @colname;
SET @table = 'mytable';
SET @s = CONCAT('SELECT ',@colname,' FROM ', @table);
PREPARE stmt FROM @s;
EXECUTE stmt;
As these correct answers suggest, you can also do this in a stored procedure, which works great for me in the MySQL 8x community:
If the string is in your external application (such as PHP), of course, just construct the MySQL statement.
Not if the string is inside a MySQL table. MySQL does not have eval() or such functions. The following is not possible:
Suppose you have a table
queries
in which fieldcolumnname
refers to one of the column names in tablemytable
.There may be additional columns in the query
that allow you to select the desiredcolumn names
.However, you can use prepared statements . Note that this is very hacky.