0

我用 A is1,2,3和 B is创建了一张表3,5,5,9,10,10

例如,我想从这个表中改变:

NP  A  B C
-- -- -- -----------------------
 1  1  3 why not
 1  1  5 weigh
 1  1  5 hello
 1  1  9 no way
 1  1 10 now or never
 1  1 10 float the boat, captain
 1  1 12 no nelp
 2  2  4 why
 2  2  6 way too much
 2  2 11 help
 3  3  1 not now
 3  3  2 not
 3  3  7 milky way
 3  3  7 this is stupid
 3  3  8 one way

变成这样:

 A   B  C
--  --  -----------------------
 1   3  why not
     5  weigh
     5  hello
     9  no way
    10  now or never
    10  float the boat, captain
    12  no nelp

 2   4  why
     6  way too much
    11  help

 3   1  not now
     2  not
     7  milky way
     7  this is stupid
     8  one way

怎么做?有人知道吗?

4

1 回答 1

2

试试这个:http ://www.sqlfiddle.com/#!2/073ec/4

select A, B, C
from
( 
  select
     if (A = @last, null, A) as A,
     B, C, (@last := A) 
  from tbl, (select @last := null as x) as vars
) as q

输出:

|      A |  B |                       C |
-----------------------------------------
|      1 |  3 |                 why not |
| (null) |  5 |                   weigh |
| (null) |  5 |                   hello |
| (null) |  9 |                  no way |
| (null) | 10 |            now or never |
| (null) | 10 | float the boat, captain |
| (null) | 12 |                 no nelp |
|      2 |  4 |                     why |
| (null) |  6 |            way too much |
| (null) | 11 |                    help |
|      3 |  1 |                 not now |
| (null) |  2 |                     not |
| (null) |  7 |               milky way |
| (null) |  7 |          this is stupid |
| (null) |  8 |                 one way |

@Naim Nsco

不需要显式声明变量,如果我在查询中显式声明它,我只是发现意图(上面的代码)更清楚。话虽如此,不声明变量也有效

select A, b, c
from
( 
  select
     if (A = @last, null, A) as A,
     B, C, (@last := A) 
  from tbl
) as q

看,它有效:http ://www.sqlfiddle.com/#!2/073ec/6

于 2012-06-18T03:41:08.260 回答