需求:
1     1
2     1
3     2
4     2
5     2
6     3
7     3
8     3
9     3结果1     1
2     1
1     2
2     2
3     2
1     3
2     3
3     3
4     3
要求:
能在MySql中运行

解决方案 »

  1.   

    select 
      (select count(1)+1 from tb where col2=t.col2 and col1<t.col1) as col1,
      col2
    from
      tb t
      

  2.   

    标准SQL基本上都支持的。mysql> select * from t_GG_wg;
    +----+------+
    | id | fld  |
    +----+------+
    |  1 |    1 |
    |  2 |    1 |
    |  3 |    2 |
    |  4 |    2 |
    |  5 |    2 |
    |  6 |    3 |
    |  7 |    3 |
    |  8 |    3 |
    |  9 |    3 |
    +----+------+
    9 rows in set (0.00 sec)mysql> select count(*),a.fld
        -> from t_GG_wg a ,t_GG_wg b
        -> where a.fld=b.fld
        -> and a.id>=b.id
        -> group by a.id
        -> order by a.id;
    +----------+------+
    | count(*) | fld  |
    +----------+------+
    |        1 |    1 |
    |        2 |    1 |
    |        1 |    2 |
    |        2 |    2 |
    |        3 |    2 |
    |        1 |    3 |
    |        2 |    3 |
    |        3 |    3 |
    |        4 |    3 |
    +----------+------+
    9 rows in set (0.00 sec)mysql>
      

  3.   

    mysql> select * from e;
    +----+------+
    | id | t    |
    +----+------+
    |  1 |    1 |
    |  2 |    1 |
    |  3 |    2 |
    |  4 |    2 |
    |  5 |    2 |
    |  6 |    3 |
    |  7 |    3 |
    |  8 |    3 |
    |  9 |    3 |
    +----+------+
    9 rows in set (0.00 sec)mysql> select count(1) cc,e.t tt from e,e ee where e.id<=ee.id and e.t=ee.t grou
    p by e.id order by tt,cc;
    +----+------+
    | cc | tt   |
    +----+------+
    |  1 |    1 |
    |  2 |    1 |
    |  1 |    2 |
    |  2 |    2 |
    |  3 |    2 |
    |  1 |    3 |
    |  2 |    3 |
    |  3 |    3 |
    |  4 |    3 |
    +----+------+
    9 rows in set (0.01 sec)
      

  4.   

    这个语法在别的数据库里面不支持吧?
    group by 列
      

  5.   

    怎么聚合函数怎么转换成hql语言啊