0

我有问题MySQL user defined variables

我的桌子是这样的..

plz    |    uid
4000        1
4000        2
9000        3
7000        4

期望的输出..

rownum  |  plz    |    uid
1000       4000         1
1000       4000         2
999        9000         3
998        7000         4

我想将SELECT一个不存在的列rownum作为标志列,以及plzuid列。如果 column 中的值plz发生变化,rownum则 column 应减少 1。所有相同的plz必须具有相同的rownumplz是独特的。

我的方法..

SELECT   
  (CASE WHEN @plz <> user.plz THEN @row := @row -1 ELSE @row END) AS rownum, @plz:= user.plz, uid
                                 
FROM             
  (SELECT @row := 1001) r, user
LEFT JOIN 
  geodb_locations ON user.plz = geodb_locations.id 
WHERE 
  user.plz IN(29386,30013,29271,30406)         
ORDER BY 
  FIELD(user.plz,29386,30013,29271,30406), 
  uid DESC

此查询有效,但仅在第二次调用时有效。第一次调用的所有值rownum都设置为 1001。如何更改CASE WHEN statement它在第一次调用时起作用的值?有任何想法吗?

提前致谢!!

编辑查询

SELECT   
            @prev as previous, @prev := u.plz as current, CASE WHEN @prev <> u.plz THEN @row := @row -1 ELSE @row END AS rownum,
            uid
                                 
            FROM             
                (SELECT @prev := NULL, @row := 1001) as r, user as u
            LEFT JOIN 
                geodb_locations ON u.plz = geodb_locations.id 
            WHERE 
                u.plz IN(29386,30013,29271,30406) AND
                freigeben=1 AND 
                uid != 97548          
            ORDER BY 
                FIELD(u.plz,29386,30013,29271,30406), 
                uid DESC

输出是正确的,但 CASE WHEN 似乎失败了!?rownum 始终为 1001!即使@prev 不等于。有任何想法吗?我的错在哪里?

输出

+----------+---------+--------+-------+
| previous | current | rownum | uid   |
+----------+---------+--------+-------+
| NULL     |   29386 |   1001 | 92014 |
| 29386    |   29386 |   1001 | 87223 |
| 29386    |   29386 |   1001 | 83160 |
| 29386    |   29386 |   1001 | 81322 |
| 29386    |   29386 |   1001 | 81293 |
| 29386    |   29386 |   1001 | 81259 |
| 29386    |   29386 |   1001 | 34213 |
| 29386    |   29386 |   1001 | 34026 |
| 29386    |   29386 |   1001 | 32851 |
| 29386    |   29386 |   1001 | 32668 |
| 29386    |   29386 |   1001 | 32358 |
| 29386    |   29386 |   1001 | 31338 |
| 29386    |   29386 |   1001 | 27765 |
| 29386    |   29386 |   1001 | 26254 |
| 29386    |   29386 |   1001 | 23474 |
| 29386    |   29386 |   1001 | 23130 |
| 29386    |   29386 |   1001 | 20512 |
| 29386    |   29386 |   1001 | 14864 |
| 29386    |   29386 |   1001 | 10951 |
| 29386    |   29386 |   1001 |  8045 |
| 29386    |   29386 |   1001 |  6921 |
| 29386    |   29386 |   1001 |  4459 |
| 29386    |   29386 |   1001 |  3152 |
| 29386    |   29386 |   1001 |  1939 |
| 29386    |   30013 |   1001 | 65193 | <-- @prev <> u.plz why NO decreasing `rownum`
| 30013    |   29384 |   1001 | 12883 |
| 29384    |   29385 |   1001 | 32667 |
| 29385    |   29232 |   1001 | 86641 |
| 29232    |   29481 |   1001 | 94521 |
| 29481    |   29481 |   1001 | 33950 |
| 29481    |   29481 |   1001 | 33926 |
| 29481    |   29667 |   1001 | 16027 |
| 29667    |   29667 |   1001 |   496 |
| 29667    |   29355 |   1001 | 96013 |
+----------+---------+--------+-------+
4

0 回答 0