Mysql
 sql >> Base de données >  >> RDS >> Mysql

Choisir dynamiquement une colonne dans MySQL

Utilisez une instruction préparée :

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)