MySQL的ORDER BY与数字用户定义的变量?
让我们首先创建一个表-
mysql> create table DemoTable1898(
Number int
);
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1898 values(10);mysql> insert into DemoTable1898 values(70);
mysql> insert into DemoTable1898 values(30);
mysql> insert into DemoTable1898 values(50);
mysql> insert into DemoTable1898 values(40);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1898;
这将产生以下输出-
+--------+| Number |
+--------+
| 10 |
| 70 |
| 30 |
| 50 |
| 40 |
+--------+
5 rows in set (0.00 sec)
这是使用数字用户定义变量执行ORDER BY的查询-
mysql> set @limitValue:=1;mysql> set @query:=CONCAT('select * from DemoTable1898 order by ',@limitValue);
mysql> prepare stmt from @query;
Statement prepared
mysql> execute stmt;
这将产生以下输出-
+--------+| Number |
+--------+
| 10 |
| 30 |
| 40 |
| 50 |
| 70 |
+--------+
5 rows in set (0.00 sec)
这是用于取消分配准备查询的查询-
mysql> deallocate prepare stmt;
以上是 MySQL的ORDER BY与数字用户定义的变量? 的全部内容, 来源链接: utcz.com/z/326777.html