mysql 在查询的结果中增加一行标记序号的伪列

dpshui 2014-01-09 11:25:17
我想在取出的数据中增加一列,把取出的数据按顺序排1,2,3,4,5...,我看有的写法是
set @rn=0;
select @rn:=@rn+1 as rn,m.name,m.code,count(t.key_tech_name) sl from tech_field_code m,data_all t
where t.PRO_TECH_FIELD=m.CODE group BY m.name ORDER BY sl desc,m.code limit 0, 10
但是这样取出来的数据 rn列并不是我想要的结果,而是12,3,1,30,4,13...一些乱七八糟的数,是不是哪理解错了,指点下,谢谢。
...全文
5945 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dpshui 2014-01-13
  • 打赏
  • 举报
回复
谢谢各位回答
wwwwb 2014-01-09
  • 打赏
  • 举报
回复
set @rn=0; select @rn:=@rn+1 as rn from ( select ,m.name,m.code,count(t.key_tech_name) sl from tech_field_code m,data_all t where t.PRO_TECH_FIELD=m.CODE group BY m.name a ORDER BY sl desc,code) a limit 0, 10
ACMAIN_CHM 2014-01-09
  • 打赏
  • 举报
回复
http://blog.csdn.net/acmain_chm/article/details/4095531 MySQL中的ROWNUM的实现 MySQL 几乎模拟了 Oracle,SQL Server等商业数据库的大部分功能,函数。但很可惜,到目前的版本(5.1.33)为止,仍没有实现ROWNUM这个功能。 下面介绍几种具体的实现方法.建立实验环境如下mysql> create table tbl ( -> id int primary key, -> col int -> );Que...
iihero_ 2014-01-09
  • 打赏
  • 举报
回复
楼上的方法正解。
phoebezhy 2014-01-09
  • 打赏
  • 举报
回复
你写成一个存储过程,然后直接在java代码里掉存储过程吧
dpshui 2014-01-09
  • 打赏
  • 举报
回复
java程序中 String sql="set @rn=0;select @rn:=@rn+1 as rn from (...."; rs=db.executeQuery(vsql,null); 这样会报错You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set @rr=0;select。。。。。 是不是executeQuery这个方法不支持?

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧