Tom: ORDER BY Variablenfrage

Beitrag lesen

Hello,

danke für die detailierten Ausführungen, in der Tat funktioniert es mit dem pma. Ich hatte auch schon an die prepared statements gedacht, allerdings keine Information darüber gefunden, ob man auch den ORDER BY-Zweig ersetzen kann. Wäre es denn so richtig:

PREPARE stmt_name FROM "SELECT * FROM testtabelle ORDER BY ?";

SET @tparm = "town";
EXECUTE stmt_name USING @tparm;

  
Ein Beispiel steht im MySQL-Manual  
  
<http://dev.mysql.com/doc/refman/5.1/en/user-variables.html>  
  
\--Zitat--:  
An exception to this principle that user variables cannot be used to provide identifiers is that if you are constructing a string for use as a prepared statement to be executed later. In this case, user variables can be used to provide any part of the statement. The following example illustrates how this can be done:  
  
mysql> SET @c = "c1";  
Query OK, 0 rows affected (0.00 sec)  
  
mysql> SET @s = CONCAT("SELECT ", @c, " FROM t");  
Query OK, 0 rows affected (0.00 sec)  
  
mysql> PREPARE stmt FROM @s;  
Query OK, 0 rows affected (0.04 sec)  
Statement prepared  
  
mysql> EXECUTE stmt;  
+----+  
| c1 |  
+----+  
|  0 |  
+----+  
|  1 |  
+----+  
2 rows in set (0.00 sec)  
  
mysql> DEALLOCATE PREPARE stmt;  
Query OK, 0 rows affected (0.00 sec)  
\--Zitat Ende--  
  
  
  
  
Liebe Grüße aus dem schönen Oberharz  
  
  
Tom vom Berg  
![](http://selfhtml.bitworks.de/Virencheck.gif)  
  

-- 
 ☻\_  
/▌  
/ \ Nur selber lernen macht schlau  
<http://bergpost.annerschbarrich.de>