在mysql中执行计算

时间:2010-09-02 04:20:30

标签: sql mysql aggregate-functions

亲爱的所有..我在DB上有一些数据。

name        Range
bla     123x9901-123x0000      //it means range 9901 until 10000 = 100
bla     123v0001-123v0100      // 10001-10100 = 100

我想要的结果如下:

   name         Qty  
   bla          200

我指望他们使用:

SELECT................
      IF(RIGHT(Range,4) = "0000",10000,RIGHT(Range,4)) - MID(Range,5,4) + 1 AS Qty
FROM mytable GROUP BY name;

但结果是:

name      Qty
bla       100

1 个答案:

答案 0 :(得分:1)

使用:

  SELECT name,
         SUM(IF(RIGHT(Range,4) = "0000",10000, RIGHT(Range,4)) - MID(Range,5,4) + 1) AS Qty
    FROM mytable
GROUP BY name;

...给我:

name    Qty  
-------------
bla     200