- 积分
- 70
- UID
- 19819
- 阅读权限
- 20
- 注册时间
- 2015-9-14
- 精华
- 在线时间
- 小时
- 最后登录
- 1970-1-1
- 职业
- 1
|
mysql 版本: 5.6.24-log
OS环境: window XP sp2
当前存储引擎为InnoDB,字符集为UTF-8
set @rownum := 0;
select id,name,@rownum as rownum
from t_two
where @rownum<=1
这样只会查询出一条记录
set @rownum := 0;
select id,name,@rownum as rownum
from t_two
where @rownum<=1
order by name,least(0,@rownum:=@rownum+1);
这样会查询出两条记录
set @rownum := 0;
select id,name,@rownum as rownum
from t_two
where id<=3
order by name,least(0,@rownum:=@rownum+1);
这样就查询出了3条记录,@rownum变量的值变为了3
explain 查看select语句为“ 全表扫描 + Using where; Using temporary; Using filesort”
order by的执行应该是在where过滤数据后,将数据放到临时表中执行的filesort,那么这里为什么写在order by中的变量会自增多次?
这个是《高性能MySQL(第3版)中文版》第284页关于用户自定义变量部分的一个例子,我不知道我忽略了哪里没能理解这里为什么执行了多次
|
|