mysql的用户变量疑问

ice_berg16 ice_berg16 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
mysql的用户变量是如何工作的?

假设有如下sql
set @cnt=0;
select @cnt:=@cnt+1,tbl.*
from tbl
会得到类似下面的数据
@cnt:=@cnt+1 | id | cid | name |
+--------------+----+-----+-------------+
| 1 | 1 | 1 | post1-1-1 |
| 2 | 2 | 1 | post1-2-2 |
| 3 | 3 | 1 | post1-3-3 |
| 4 | 4 | 1 | post1-4-4 |
| 5 | 5 | 1 | post1-5-5 |
| 6 | 6 | 2 | post2-1-6 |
| 7 | 7 | 2 | post2-2-7 |
| 8 | 8 | 3 | post3-1-8 |

而使用
set @cnt=0;
select @cnt:=@cnt+1,tbl.*
from tbl
where @cnt=1;则没有记录输出
如果条件改成@cnt=0则会输出第一条记录,不知道MYSQL是计算处理变量的
...全文

相关推荐

评论 17
收藏 已收藏
分享
返回顶部
ice_berg16
基础编程 21,889 成员 140,329 内容