如 有表
id     字段1     字段21 a
2 fsf
3 fe
4 ggvbc
5 fsdf
6 fd
7 fsdf
8 b
9 kjf
10 jty
11 jty
12 ty
13 c
14 kyutj
15 jty
16 d
17 hfsh
18 berea 下面的内容  就是 与a 有关的,b 下面的内容是与b 有关的
所以我需要得到的是这样的
id     字段1     字段2
1 a
2 a fsf
3 a fe
4 a ggvbc
5 a fsdf
6 a fd
7 a fsdf
8 b
9 b kjf
10 b jty
11 b jty
12 b ty
13 c
14 c kyutj
15 c jty
16 d
17 d hfsh
18 d bere哪位大侠帮帮忙

解决方案 »

  1.   

    mysql> select * from t_fengchujun;
    +------+------+-------+
    | id   | col1 | col2  |
    +------+------+-------+
    |    1 | a    | NULL  |
    |    2 | NULL | fsf   |
    |    3 | NULL | fe    |
    |    4 | NULL | ggvbc |
    |    5 | NULL | fsdf  |
    |    6 | NULL | fd    |
    |    7 | NULL | fsdf  |
    |    8 | b    | NULL  |
    |    9 | NULL | kjf   |
    |   10 | NULL | jty   |
    |   11 | NULL | jty   |
    |   12 | NULL | ty    |
    |   13 | c    | NULL  |
    |   14 | NULL | kyutj |
    |   15 | NULL | jty   |
    |   16 | d    | NULL  |
    |   17 | NULL | hfsh  |
    |   18 | NULL | bere  |
    +------+------+-------+
    18 rows in set (0.00 sec)mysql> select id,
        ->  (select col1 from t_fengchujun where id=(select max(id) from t_fengchujun where id<=a.id and col1 is not null)) as col1,
        ->  col2
        -> from t_fengchujun a;
    +------+------+-------+
    | id   | col1 | col2  |
    +------+------+-------+
    |    1 | a    | NULL  |
    |    2 | a    | fsf   |
    |    3 | a    | fe    |
    |    4 | a    | ggvbc |
    |    5 | a    | fsdf  |
    |    6 | a    | fd    |
    |    7 | a    | fsdf  |
    |    8 | b    | NULL  |
    |    9 | b    | kjf   |
    |   10 | b    | jty   |
    |   11 | b    | jty   |
    |   12 | b    | ty    |
    |   13 | c    | NULL  |
    |   14 | c    | kyutj |
    |   15 | c    | jty   |
    |   16 | d    | NULL  |
    |   17 | d    | hfsh  |
    |   18 | d    | bere  |
    +------+------+-------+
    18 rows in set (0.01 sec)mysql>
      

  2.   


    SELECT t1.id,t2.field1,t1.字段2 FROM tablename t1 INNER JOIN
    (SELECT id,字段1 field1 FROM tablename WHERE length(字段1)=1) t2
    WHERE t1.id<=t2.id;
      

  3.   


    SELECT t1.id,t2.field1,t1.字段2 FROM tablename t1 INNER JOIN
    (SELECT id,字段1 field1 FROM tablename WHERE length(字段1)=1) t2
    ON t1.id<=t2.id;
      

  4.   

    谢谢ACMAIN_CHM  大哥 ,问题已解决还想问问,,能说一下  逻辑 吗实在是,不太理解
      

  5.   

     (select col1 from t_fengchujun where id=(select max(id) from t_fengchujun where id<=a.id and col1 is not null)) as col1,
     
     选出 id=(select max(id) from t_fengchujun where id<=a.id and col1 is not null) 的记录的col1值
     选出比当前记录a.id小的最大的id, select max(id) from t_fengchujun where id<=a.id and col1 is not null