Mysql
 sql >> Database >  >> RDS >> Mysql

Scelta dinamica di una colonna in MySQL

Usa una dichiarazione preparata:

mysql> SET @sql = CONCAT("SELECT ", (SELECT "NOW()"));
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @sql;
+--------------+
| @sql         |
+--------------+
| SELECT NOW() | 
+--------------+
1 row in set (0.00 sec)

mysql> PREPARE stmt FROM @sql;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> EXECUTE stmt;
+---------------------+
| NOW()               |
+---------------------+
| 2009-04-06 23:08:31 | 
+---------------------+
1 row in set (0.00 sec)