现在有一个表,名字就叫作comment吧:
+------+
|    id  |
+------+
|   102 |
|   203 |
|   213 |
|   222 |
|   258 |
|   288 |
|   298 |
|   322 |
+------+我想给他按照id顺序加上编号:+---+-----+
| 编号 | id | 
+---+-----+
|    1 | 102 |
|    2 | 203 |
|    3 | 213 |
|    4 | 222 |
|    5 | 258 |
|    6 | 288 |
|    7 | 298 |
|    8 | 322 |
+----+----+
这个我能用以下语句实现:
set @floor := 0; select @floor := @floor + 1 as '编号', comment.* from comment然后我想得到编号4和其后面的2行,在这张表里就会得到如下结果:+---+-----+
| 编号 | id | 
+---+-----+
|    4 | 222 |
|    5 | 258 |
|    6 | 288 |
+----+----+按照这个实例的步骤,我想请问一般情况下的MySql语句怎么写。我才接触Sql,希望大家能帮助我。谢谢

解决方案 »

  1.   

    select 编号,id From temp where 编号 >=4 And 编号 <= 4+2
    不行吗?呵呵
      

  2.   

    SQL不好写,用存储过程或则函数什么的就简单了。
      

  3.   

    ----增加id
    alter table comment add column id int auto_increment not null;
    --到编号4和其后面的2行select * from comment order by id limit 4,2;
      

  4.   

    mysql> SELECT * FROM TA4;
    +------+
    | ID   |
    +------+
    |  102 |
    |  203 |
    |  213 |
    |  222 |
    |  258 |
    |  288 |
    |  298 |
    |  322 |
    +------+
    8 rows in set (0.00 sec)mysql> SET @NUM:= 0;
    Query OK, 0 rows affected (0.00 sec)mysql> SELECT * FROM (
        -> SELECT *,@NUM:= @NUM + 1 AS '编号' FROM TA4) A ORDER BY 编号 LIMIT 3,3;
    +------+------+
    | ID   | 编号     |
    +------+------+
    |  222 |    4 |
    |  258 |    5 |
    |  288 |    6 |
    +------+------+
    3 rows in set (0.00 sec)mysql>
      

  5.   

    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...
      

  6.   

    set @floor := 0; select * from (select @floor := @floor + 1 as `编号`, comment.* from comment) t where `编号`>=4 limit 3;
      

  7.   

    or
    mysql> SET @NUM:= 0;
    Query OK, 0 rows affected (0.00 sec)mysql> SELECT * FROM (SELECT *,@NUM:= @NUM + 1 AS '编号' FROM TA4) A
        -> WHERE 编号>=4 ORDER BY 编号 LIMIT 3;
    +------+------+
    | ID   | 编号     |
    +------+------+
    |  222 |    4 |
    |  258 |    5 |
    |  288 |    6 |
    +------+------+
    3 rows in set (0.00 sec)mysql>
      

  8.   

    谢谢!
    wwwwb在5楼的回复很好地解决了问题:mysql> set @num:=0; select floor from (select floor, @num:=@num+1 as '编号' from comment order by floor) a order by '编号' limit 2,3;
    Query OK, 0 rows affected (0.00 sec)+-------+
    | floor |
    +-------+
    |   102 |
    |   203 |
    |   213 |
    +-------+
    3 rows in set (0.00 sec)但是7楼和wwwwb在8楼的语句我试了一下,在我这里没有结果,不知道什么原因:
    set @num:=0; select * from (SELECT *, @num:= @num + 1 AS '编号' FROM comment) t where '编号'>4 limit 3;
    Query OK, 0 rows affected (0.00 sec)Empty set, 1 warning (0.00 sec)mysql> set @num:=0; select * from (select *, @num:=@num+1 as '编号' from comment) a where '编 号'>4 order by '编号' limit 3;
    Query OK, 0 rows affected (0.00 sec)Empty set, 1 warning (0.00 sec)我先给分了,不过想问一下wwwwb,有什么好的SQL书籍推荐一下吗?谢谢你了
      

  9.   

    我是用你1楼的数据测试的,没有问题,
    SHOW WARNINGS
    看看结果sql cookbook
    高性能 mysql
      

  10.   


    mysql> show warnings;
    +---------+------+-----------------------------------------+
    | Level   | Code | Message                                 |
    +---------+------+-----------------------------------------+
    | Warning | 1292 | Truncated incorrect DOUBLE value: '? ?' |
    +---------+------+-----------------------------------------+
    1 row in set (0.08 sec)
      

  11.   

    贴建表及插入记录的SQL,及要求结果出来看看