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)
这是使用数字用户定义变量执行ORDERBY的查询-
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;
热门推荐
10 新年元旦祝福语大全简短
11 新年探亲祝福语简短精辟
12 对别人新年祝福语简短
13 对同学祝福语 简短独特
14 舞台上祝福语大全简短
15 同事调动的祝福语简短
16 公司新年祝福语简短的
17 哥哥结婚的祝福语简短
18 送离职同事简短祝福语