tags:

views:

23

answers:

2

I have a variable holding the column name. I want to do a select with it like this way

select `@x` from table;

Is it possible?

+1  A: 

You can use a prepared statement as follows:

SET @x = 'some_field';

SET @s = CONCAT('SELECT ', @x, ' FROM table;');

PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Daniel Vassallo
+1  A: 

This should work:

SET @y = CONCAT('SELECT ', @x, ' FROM table;');
PREPARE stmt1 FROM @y;
EXECUTE stmt1;
skyman
...had too many tabs ;/
skyman
You need a space before your `FROM`
ck